Skip to content Skip to sidebar Skip to footer

Parsing Json In Nodejs

Hi i have the below json {id:'12',data:'123556',details:{'name':'alan','age':'12'}} i used the code below to parse var chunk={id:'12',data:'123556',details:{'name':'alan','age':'1

Solution 1:

If you already have an object, you don't need to parse it.

var chunk={id:"12",data:"123556",details:{"name":"alan","age":"12"}};
// chunk is already an object!

console.log(chunk.details);
// => {"name":"alan","age":"12"}

console.log(chunk.details.name);
//=> "alan"

You only use JSON.parse() when dealing with an actual json string. For example:

var str = '{"foo": "bar"}';
console.log(str.foo);
//=> undefined// parse str into an objectvar obj = JSON.parse(str);

console.log(obj.foo);
//=> "bar" 

See json.org for more details

Solution 2:

Since jsonobj has already been parsed as a JavaScript Object, jsonobj.details.name should be what you need.

Post a Comment for "Parsing Json In Nodejs"