How to access nested JSON data
To be honest, I can't understand your problem. JSON is already structured out, why do you need to change the structure?
In you case, I would access it as follows:
data.address.streetName;
If, by any chance, what you want is to traverse the data, you would need:
function traverse_it(obj){
for(var prop in obj){
if(typeof obj[prop]=='object'){
// object
traverse_it(obj[prop[i]]);
}else{
// something else
alert('The value of '+prop+' is '+obj[prop]+'.');
}
}
}
traverse_it(data);
Update
After reading below, what this user needs seems more obvious. Given property names as a string, s/he wants to access the object.
function findProp(obj, prop, defval){
if (typeof defval == 'undefined') defval = null;
prop = prop.split('.');
for (var i = 0; i < prop.length; i++) {
if(typeof obj[prop[i]] == 'undefined')
return defval;
obj = obj[prop[i]];
}
return obj;
}
var data = {"id":1,"name":"abc","address":{"streetName":"cde","streetId":2}};
var props = 'address.streetName';
alert('The value of ' + props + ' is ' + findProp(data, props));
If you use lodash(a very popular utility library), you can use _.get()
.
e.g.
var data = {
"id":1,
"name": "abc",
"address": {
"streetName": "cde",
"streetId":2
}
}
_.get(data, 'address.streetName');
// 'cde'
_.get(data, ['address', 'streetName']);
// 'cde'
If it involves an array, you can use string path like 'address[0].streetName'
as well.
e.g.
var data = {
"id":1,
"name": "abc",
"addresses": [
{
"streetName": "cde",
"streetId": 2
},
{
"streetName": "xyz",
"streetId": 102
},
]
}
_.get(data, 'addresses[0].streetName');
// cde
_.get(data, [address, 1, streetName]);
// xyz
Internally, it uses toPath()
function to convert string path (e.g. address.streetName
) into an array (e.g. ['address', 'streetName']
), and then uses a function to access the data at the given path within the object.
Other similar utility functions include _.set()
and _.has()
. Check them out.