Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of stringify and then parse a JSON object

Is there any specific reason for stringifya JSON object and parseit again. Obviously it will return the Initial object itself. is there advantage of doing this?

Code 1: stringify and then parse

 var textstring = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj=JSON.parse(textstring);
var obj2=JSON.parse(JSON.stringify(obj));

code 2:Direct Use

var textstring = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj=JSON.parse(textstring);
var obj2=obj;
like image 538
Venkat Avatar asked Apr 19 '16 06:04

Venkat


People also ask

What is the use of JSON Stringify and JSON parse?

JSON. stringify() takes a JavaScript object and then transforms it into a JSON string. JSON. parse() takes a JSON string and then transforms it into a JavaScript object.

What is the use of parse JSON?

JSON.parse() A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse() , and the data becomes a JavaScript object.

What happens if you JSON parse an object?

JSON. parse() converts any JSON string passed into the function into a JSON object.

What does it mean to parse an object?

"Parsing is to read the value of one object to convert it to another type". Well that's only part of what parsing is. Parsing is the process of taking data (text) and determining its meaning based on a set of rules.


1 Answers

I think you may have a fundamental misunderstanding. JSON is a textual notation for data exchange. If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.

You don't "take a JSON object and stringify it." If it's an object, it's not JSON; if it's JSON notation for an object, it's a string and you wouldn't stringify it.

Here's an object:

var foo = {answer: 42};

Here's using stringify on it:

var str = JSON.stringify(foo);

Now str is a string containing JSON, with this content:

{"answer":42}

...exactly as though you had written this:

var str = '{"answer":42}';

You can parse that back into an object (note: not a "JSON object," just an object):

var foo2 = JSON.parse(str);

Now, foo refers to the original object, and foo2 refers to a different object with copies of the properties:

console.log(foo == foo2);               // false, they're different object
console.log(foo.answer == foo2.answer); // true, they each have an answer property
                                        // and their values match
console.log(foo.answer);                // 42
console.log(foo2.answer);               // 42
foo2.answer = 67;
console.log(foo.answer);                // 42 | the objects and their properties
console.log(foo2.answer);               // 67 | are not connected in any way

Is there any specific reason for stringify a JSON object and parse it again.

Sometimes people use that as a poor man's cloning method, as the object you get back is not the same object you stringified; it's a new object with the same properties (provided that all of the properties of the original could be serialized to JSON; properties referring to functions or with the value undefined can't be, and many other values [such as dates] don't convert back without a "reviver" function for JSON.parse, so they end up being strings or numbers).

That fits with the code in the latest version of your question:

Code 1: stringify and then parse

var textstring = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj=JSON.parse(textstring);
var obj2=JSON.parse(JSON.stringify(obj));

Code 2:Direct Use

var textstring = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj=JSON.parse(textstring);
var obj2=obj;

(Note I changed text and text2 to obj and obj2; they aren't text.)

At the end of Code 1, obj and obj2 refer to different objects. If you change one of the properties on the object obj refers to, obj2 is completely unchanged:

// Replace the employees array with a blank one
obj.employees = [];

// No effect at all on obj2:
console.log(obj2.employees[0].firstName); // "John"

In contrast, of course, with Code 2 obj and obj2 are both references to the same object.

like image 174
T.J. Crowder Avatar answered Sep 22 '22 07:09

T.J. Crowder