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 Overflow Top answer 1 of 16
715
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"
2 of 16
61
JSON.parse() is for "parsing" something that was received as JSON.
JSON.stringify() is to create a JSON string out of an object/array.
Videos
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › parse
JSON.parse() - JavaScript | MDN
Similar to the replacer parameter of JSON.stringify(), for arrays and objects, reviver will be last called on the root value with an empty string as the key and the root object as the value. For other valid JSON values, reviver works similarly and is called once with an empty string as the key and the value itself as the value. If you return another value from reviver, that value will completely replace the originally parsed value. This even applies to the root value. For example...
Medium
medium.com › @ronaldssebalamu › json-parse-vs-json-stringify-ad2d3218bc44
JSON.parse() vs JSON.stringify(). Today we are yet to delve into one of… | by Ronald Ssebalamu | Medium
September 13, 2023 - const firstString = ` { "name": "ronald", "description": "Developer" } ` console.log(JSON.parse(firstString)) //output //{"name": "ronald", "description": "Developer"} Serializes the JavaScript object into a JSON-formatted string. Input: JavaScript object. Output: JSON-formatted string ... const firstArray = [1, 2, 3, 5, 7, 11] console.log(JSON.stringify(firstArray)) // Output // `[1, 2, 3, 5, 7, 11]`
Medium
medium.com › @debbie.obrien › json-parse-v-json-stringify-4b9d104c78d0
JSON Parse v JSON Stringify. I always get confused between the JSON… | by Debbie O'Brien | Medium
May 28, 2018 - The JQuery SerializeArray() method creates an array of obejcts by serializing form values. It is very similar to the serialize() method but returns a JSON data structure. This is returned not as a string and if you needed it to be a string you would have to call the stringify method on it.
DigitalOcean
digitalocean.com › community › tutorials › js-json-parse-stringify
How To Use JSON.parse() and JSON.stringify() | DigitalOcean
November 24, 2021 - let userStr = '{"name":"Sammy","email":"sammy@example.com","plan":"Pro"}'; let userObj = JSON.parse(userStr, (key, value) => { if (typeof value === 'string') { return value.toUpperCase(); } return value; }); console.log(userObj); Executing this code will produce the following output: Output{name: 'SAMMY', email: 'SAMMY@EXAMPLE.COM', plan: 'PRO'} email: "SAMMY@EXAMPLE.COM" name: "SAMMY" plan: "PRO" The values have been transformed to uppercase characters. JSON.stringify() takes a JavaScript object and transforms it into a JSON string.
Top answer 1 of 2
7
Hii Shubham, · They are the complete opposite of each other. · JSON.parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. · JSON.stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a . · JSON.stringify(obj) - Takes any serializable object and returns the JSON representation as a string. · JSON.stringify() -> ObjectToString. · JSON.parse(string) - Takes a well formed JSON string and returns the corresponding JavaScript object. · JSON.parse() -> StringToObjectYou can also refer to this link for more details:- https://javascript.plainenglish.io/how-to-use-stringify-and-parse-in-javascript-6b637b571a32 · Please mark my answer Helpful/Correct, if it is an impactfulRegards,Surbhi
2 of 2
5
Hello, · 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. · for Realtime usecases :- · https://community.servicenow.com/community?id=community_blog&sys_id=aedbb9e8dbafbb805ed4a851ca9619ba · Please mark my answer Helpful/Correct, if it is an impactful · · Regards, · Vaishnavi Lathkar
LinkedIn
linkedin.com › pulse › understanding-jsonstringify-jsonparse-javascript-shaharyar-saleem
Understanding JSON.stringify() and JSON.parse() in JavaScript
July 3, 2023 - Customization: The function accepts additional parameters that allow for customization. For example, you can specify a replacer function to filter or transform the data before stringifying, or use the "space" parameter to add indentation for improved readability. ... JSON.parse() is the counterpart of JSON.stringify().
Team Treehouse
teamtreehouse.com › community › what-is-the-difference-between-json-and-jsonparse
what is the difference between json() and json.parse()? (Example) | Treehouse Community
April 6, 2021 - Keep in mind JSON.parse() is used to turn AJAX response JSON data (sent as a string) into a JSON object in the response handler code, so the values can be extracted efficiently/expediently from the JOSN (via the object attributes or key/value pairs). ... Also, be aware of JSON.stringify which does the opposite.
Studytonight
studytonight.com › forum › difference-between-jsonstringify-and-jsonparse
Difference between JSON.stringify and JSON.parse - Studytonight
JSON.stringify({}); // '{}' JSON.stringify(true); // 'true' JSON.stringify('foo'); // '"foo"' JSON.stringify([1, 'false', false]); // '[1,"false",false]' JSON.stringify({ x: 5 }); // '{"x":5}' JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) // '"2006-01-02T15:04:05.000Z"' JSON.stringify({ x: 5, y: 6 }); // '{"x":5,"y":6}' or '{"y":6,"x":5}' JSON.stringify([new Number(1), new String('false'), new Boolean(false)]); // '[1,"false",false]' JSON.parse() The JSON.parse() method parses a string as JSON, optionally transforming the value produced.