JSON is a very popular format in programming and support by most of modern languages. In Javascript, it natively supports JSON.
Parse JSON text to Javascript object
We have a variable assigned to this JSON text.
var jsonin = '{"employees":[ {"firstName":"Config9", "lastName":"COM"}, {"firstName":"Inneka", "lastName":"COM"}, {"firstName":"oraerr", "lastName":"COM"} ]}';
Now use JSON.parse() with this text
var jsonobj = JSON.parse(jsonin);
Now we have an object, we can access elements in this object as normal.
console.log(jsonobj.employees[1].firstName);
Dump JSON object to text output
we can use JSON.stringify() method to dump JSON object as follow:
var jsontxt = JSON.stringify(jsonobj, undefined, 4); console.log(jsontxt); console.log(typeof(jsontxt));
Happy coding!