Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JSON.parse() work?

I have not worked too much on javascript. And, I need to parse a JSON string. So, I want to know what exactly JSON.parse does. For example : If I assign a json string to a variable like this,

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

Now when I print 'ab', I get an object.

Similarly when I do this :

var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';
var rs = JSON.parse(pq);

The 'rs' is the same object as 'ab'. So what is the difference in two approaches and what does JSON.parse did differently ?

This might be a silly question. But it would be helpful if anybody can explain this.

Thanks.

like image 492
rsKRISH Avatar asked Sep 22 '15 07:09

rsKRISH


People also ask

Is JSON parse the same as JSON ()?

The difference is: json() is asynchronous and returns a Promise object that resolves to a JavaScript object. JSON. parse() is synchronous can parse a string to (a) JavaScript object(s).

How does parse work in JavaScript?

A parser basically reads our code line by line and checks it for errors and stops execution if any, if the code is free of syntactic errors, the parser produces a data structure called abstract syntax tree, which is then translated to machine code and thus results to output.

What does JSON parser return?

The JSON. parse() method parses a string and returns a JavaScript object. The string has to be written in JSON format.

Does JSON parse automatically?

The JSON file will be parsed for you automatically and you can start using it in your project: const jokes = require('./jokes. json'); console.


2 Answers

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.

like image 62
Shailendra Sharma Avatar answered Oct 02 '22 14:10

Shailendra Sharma


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 object
var 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 object
var 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.

like image 29
Sotiris Kiritsis Avatar answered Oct 02 '22 13:10

Sotiris Kiritsis