Venkat
Venkat

Reputation: 2579

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;

Upvotes: 11

Views: 11017

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075079

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.

Upvotes: 25

Related Questions