Fred van Rijswijk Fred van Rijswijk - 1 month ago 17
JSON Question

Multi level json with body response in nodjs

I make a request to REST Server with NodeJS and getting this type of JSON in my 'body'

[{
"id": 802,
"created": "2016-10-18 15:22:08",
"test": {
"name": "Fred "
}
}]


log my body with JSON.parse, that is working fine for the 'root' of the JSON, but I'm not getting the 'name' in the 'test' array

var jsonObj = JSON.parse(body);
for(index in jsonObj) {
console.log(jsonObj[index].bag);


How can I make the loop and if there is a 'test' getting the data keys out ??

Answer

This code should work given your JSON description

var jsonObj = JSON.parse(body);
for(index in jsonObj) {
    console.log(jsonObj[index].test.name);
Comments