Convert JSON to JavaScript Objects

JSON is the representation of a JavaScript object in string form. We can use the JSON.parse(string) method to convert a string that is properly formatted with JSON into a JavaScript object.

var team = '{"name":"RajTeam", "members":["Raj","Kamal"], \
                   "number":123, "location": "Bangalore"}';
var teamObj = JSON.parse(team);
console.log(teamObj.name);
console.log(teamObj.members);

This is the output from the code above:

RajTeam
[ 'Raj', 'Kamal' ]

Enjoyed this post? Share it!