How does JSON.parse() work? How does JSON.parse() work? json json

How does JSON.parse() work?


A Javascript object is a data type in Javascript - it's have property and value pair as you define in your first example.

var ab = {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}};

Now What is Json : A JSON string is a data interchange format - it is nothing more than a bunch of characters formatted a particular way (in order for different programs to communicate with each other)

var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';

so it's is a String With json Format.

and at last JSON.parse() Returns the Object corresponding to the given JSON text.


Here is my explanation with a jsfiddle.

//this is already a valid javascript object//no need for you to use JSON.parse()var obj1 = {"name":"abcd", "details":"1234"};console.log(obj1);//assume you want to pass a json* in your code with an ajax request//you will receive a string formatted like a javascript objectvar str1 = '{"name":"abcd", "details":"1234"}';console.log(str1);//in your code you probably want to treat it as an object//so in order to do so you will use JSON.parse(), which will//parse the string into a javascript objectvar obj2 = JSON.parse(str1);console.log(obj2);

JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML.


Your 'ab' variable isn't a string, it is a proper javascript object, since you used the {} around it. If you encased the whole thing in "" then it would be a string and would print out as a single line.