9

i have a JSON variable which looks like this :

{"events": [
{"event_id": "1", "event_name": "Breakfast"},
{"event_id": "1", "event_name": "Calling Bob"}
]};

i have two vaiables

x=2; y=jam;

i want to push the variables in json such that x is event_id and y is event_name, so that my json will look like-

{"events": [
{"event_id": "1", "event_name": "Breakfast"},
{"event_id": "1", "event_name": "Calling Bob"},
{"event_id": "2", "event_name": "jam"}
]};

the function iam using to push is

k='({"event_id":"'+x+'","event_name":"'+y+'"})';
san.events.push(k);

where san is the variable in which i stored the json. iam parsing the variable san and applying the push action and stringifying it and displaying it, but in the result my json data syntax is changing like additional '/' symbols are generating in the json.

1
  • 5
    Don't build your own JSON strings, use the JSON.stringify() and JSON.parse() methods on actual objects and arrays (see MDN). Plus the (), what are those for? Commented Jun 21, 2014 at 12:45

5 Answers 5

6

JSON objects are first class citizens in JavaScript, you can use them as literals.

var k= {"event_id": x, "event_name":y};
san.events.push(k);

As the above is the exact same thing as:

var k = new Object();
k.event_d = x;
k.event_name = y;
san.events.push(k);

JSON is just a way to represent regular objects in JavaScript.

If you really want to transform strings into JSON, you can use the JSON.parse() method, but you normally want to avoid this.

EDIT

As @Alvaro pointed out, even though you can create regular Objects using the JSON syntax, it's not synonym of Object, it' just a way of representing the Object data, and not just any data, JSON cannot represent recursive objects.

Sign up to request clarification or add additional context in comments.

6 Comments

First class citizens?
It means that they "exist", they are recognized by the JavaScript engine in your browser.
Please don't use JSON as synonym of object. JSON is a string serialization format. If it isn't a string, it can't be JSON in the first place.
And why would you want to "avoid" using JSON.parse()? You use it if you need it; saying avoid it is bad "advice".
@AndréPena JSON object is an intrinsic object in browsers, it has methods like parse and stringify. k in your code is a very regular JS object, also called "Object initializer" or "Object literal", it has nothing to do with JSON.
|
4

The variable value is missing Quote here

x=2; y='jam';

and change push code like this

k={"event_id":"'+x+'","event_name":"'+y+'"};
san.events.push(k);

2 Comments

And it'll push as a string and x and y will be just that, x and y, not the variable values.
if i push using this method and alert(san); im getting unessassary / symbols.
2

Pass it inside a square bracket [variable_name]

Example:

let account_name = "premium_account"
{"features": { [account_name] :"checked"}}

1 Comment

This is really helpful. I had a situation where i wanted to declare objects inline in a vue template. I can pass the variable straight into the object like this :path="{ 'Dashboard': route('dashboard'), [var.detail] : false }"
1

Does this work for you?

x = "2";
y = "jam"
k= {"event_id": x, "event_name": y };
san.events.push(k);

Comments

1

here is solution for this problem.

san = {"events": [
{"event_id": "1", "event_name": "Breakfast"},
{"event_id": "1", "event_name": "Calling Bob"}
]};
var x=2; var y='am';
k={"event_id":"x","event_name":y};
san.events.push(k);
console.log(san);

http://jsfiddle.net/3NSy8/

1 Comment

to see the console result go to the link the press F12 then click jsfiddle run button and show result in inspect element console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.