Is there any specific reason for stringify
a JSON object and parse
it 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;
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.
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.
JSON. parse() converts any JSON string passed into the function into a JSON 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.
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 andparse
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With