I need to add an attribute which doesn't exist in the current JSON. The json object looks like below.
var jsonObj = {
"result" : "OK",
"data" : []
};
And I want to add temperature inside 'data'. I could do it like below.
jsonObj.data.push( {temperature : {}} );
And then, I want to add 'home', 'work' inside 'temperature'. The result would be like below.
{
"result" : "OK",
"data" : [
{ "temperature" : {
"home" : 24,
"work" : 20 } }
]
};
How could I do this? I succeeded to insert 'temperature' inside 'data', but couldn't add 'home' & 'work' inside temperature. There could be more inside the 'temperature' so it needs to be enclosed with {}.
I need to add an attribute which doesn't exist in the current JSON. The json object looks like below.
var jsonObj = {
"result" : "OK",
"data" : []
};
And I want to add temperature inside 'data'. I could do it like below.
jsonObj.data.push( {temperature : {}} );
And then, I want to add 'home', 'work' inside 'temperature'. The result would be like below.
{
"result" : "OK",
"data" : [
{ "temperature" : {
"home" : 24,
"work" : 20 } }
]
};
How could I do this? I succeeded to insert 'temperature' inside 'data', but couldn't add 'home' & 'work' inside temperature. There could be more inside the 'temperature' so it needs to be enclosed with {}.
Share Improve this question asked Apr 22, 2014 at 1:10 MingooMingoo 4772 gold badges7 silver badges17 bronze badges 4- 1 It's confusing, but the term "JSON object" is a misnomer. In JavaScript, an object literal creates a plain old JavaScript object. The syntax is a superset of JSON syntax. – Pointy Commented Apr 22, 2014 at 1:13
- @Pointy Thanks Pointy. Can I ask what makes this object a superset of JSON? (I thought this is a completely JSON object.) – Mingoo Commented Apr 22, 2014 at 1:36
- 1 Well because you're working in JavaScript, there is only one kind of object, and that's a JavaScript object :) The JavaScript syntax for creating objects ("object literal" expressions) has existed for longer than the idea of JSON. In other languages, you'd normally construct JSON as a string. In JavaScript the syntax that looks like JSON syntax is just plain JavaScript syntax in reality, and you can do more with it than you can with JSON - like, have functions as property values, for example. – Pointy Commented Apr 22, 2014 at 1:39
- @Pointy I see. I'm actually using Node.js but I guess it's JavaScript also, right? Then I think I need to go and find out the definition of the JSON exactly. Thanks for the comment. If you have any more comments, please leave some. I would appreciate it. – Mingoo Commented Apr 22, 2014 at 5:17
2 Answers
Reset to default 15How about this?
var temperature = { temperature: { home: 24, work: 20 }};
jsonObj.data.push(temperature);
I can't tell if doing it in two steps is important by how your question is structured, but you could add the home and work properties later by indexing into the array, like this:
jsonObj.data.push({ temperature: { }});
jsonObj.data[0].temperature.home = 24;
jsonObj.data[0].temperature.work = 20;
But, that's not a great idea since it depends on the array index to find the temperature object. If that's a requirement, it would be better to do something like loop through the data
array to find the object you're interested in conclusively.
Edit:
An example of looping through to locate the temperature object would go something like this:
for (var i = 0; i < jsonObj.data.length; i++) {
if (jsonObj.data[i].temperature) {
break;
}
}
jsonObj.data[i].temperature.home = 24;
jsonObj.data[i].temperature.work = 20;
Edit:
If which particular property you'll be interested in is unknown at development time, you can use bracket syntax to vary that:
for (var i = 0; i < jsonObj.data.length; i++) {
if (jsonObj.data[i]['temperature']) {
break;
}
}
jsonObj.data[i]['temperature'].home = 24;
jsonObj.data[i]['temperature'].work = 20;
Which means you could use a variable there instead of hard coding it:
var target = 'temperature';
for (var i = 0; i < jsonObj.data.length; i++) {
if (jsonObj.data[i][target]) {
break;
}
}
jsonObj.data[i][target].home = 24;
jsonObj.data[i][target].work = 20;
How about
jsonObj.data.push( {
temperature : {
"home" : 24,
"work" : 20
}
} );