JSON.stringify turns a JavaScript object into JSON text and stores that JSON text in a string, eg:
var my_object = { key_1: "some text", key_2: true, key_3: 5 };
var object_as_string = JSON.stringify(my_object);
// "{"key_1":"some text","key_2":true,"key_3":5}"
typeof(object_as_string);
// "string"
JSON.parse turns a string of JSON text into a JavaScript object, eg:
var object_as_string_as_object = JSON.parse(object_as_string);
// {key_1: "some text", key_2: true, key_3: 5}
typeof(object_as_string_as_object);
// "object"
Answer from Quentin on Stack OverflowJSON.stringify turns a JavaScript object into JSON text and stores that JSON text in a string, eg:
var my_object = { key_1: "some text", key_2: true, key_3: 5 };
var object_as_string = JSON.stringify(my_object);
// "{"key_1":"some text","key_2":true,"key_3":5}"
typeof(object_as_string);
// "string"
JSON.parse turns a string of JSON text into a JavaScript object, eg:
var object_as_string_as_object = JSON.parse(object_as_string);
// {key_1: "some text", key_2: true, key_3: 5}
typeof(object_as_string_as_object);
// "object"
JSON.parse() is for "parsing" something that was received as JSON.
JSON.stringify() is to create a JSON string out of an object/array.
JSON.parse(JSON.stringify(x)) Purpose?
Alternative of JSON.parse(JSON.stringify(data)) as its a synchronous function
Trying to learn fetch(), JSON.stringify() confusion
What is happening when I call JSON and JSON.stringify in the console?
It's a way of cloning an object, so that you get a complete copy that is unique but has the same properties as the cloned object.
var defaultParams = { a : 'b' };
var params = JSON.parse(JSON.stringify(defaultParams));
console.log( params.a ); // b
console.log( defaultParams.a ); // b
console.log( params === defaultParams ); // false
The above outputs false because even though both objects have the a property, with the value b, there are different objects that are independent of each other (they don't refer to the same reference).
The JSON method will only work with basic properties - no functions or methods.
You can break the connection between two arrays.
For example:
const bulkAssignTreeView = JSON.stringify(this.bulkAssignTreeViewData);
this.bulkAssignTreeViewData = JSON.parse(bulkAssignTreeView);