11

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;
Venkat
  • 2,549
  • 2
  • 28
  • 61
  • *"Is there any specific reason for `stringify` a JSON object and `parse` it again."* The code you added does the opposite of what you describe. – T.J. Crowder Apr 19 '16 at 06:38
  • Your *updated* code makes basically no sense. You're passing a **string** into `JSON.stringify`. – T.J. Crowder Apr 19 '16 at 06:41

1 Answers1

24

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • is there any advantage in stringifying an object when sending it over the network, in terms of size? I can image a string is smaller than an object. On the other hand it adds the cost of converting every time. – The Fool Nov 17 '19 at 11:42
  • 1
    @TheFool - You can't send an object over the network, you have to convert it to a series of bytes that can be used at the other end. There is no standard binary format for objects, so we use any of several structured textual formats. JSON is one of those. – T.J. Crowder Nov 18 '19 at 07:53
  • Thanks for your answer. I am not sure what happens internally but I am very sure I can send object without stringifying explicitly. If I receive them, in js,I can use them just like that. If I stringify, I have to parse them first. So to be it looks like both options are available. – The Fool Nov 18 '19 at 08:05
  • @TheFool - Then *something* is doing it for you (at both ends). Again: There is no standard binary format for objects. – T.J. Crowder Nov 18 '19 at 08:06
  • That is interesting because I experience this behaviour pretty much with every lib I use, js and python. Not sure about golang atm. – The Fool Nov 18 '19 at 08:08