Difference between JSON.stringify and JSON.parse Difference between JSON.stringify and JSON.parse json json

Difference between JSON.stringify and JSON.parse


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" 


JSON.parse() is for "parsing" something that was received as JSON.
JSON.stringify() is to create a JSON string out of an object/array.


They are the inverse of each other. JSON.stringify() serializes a JS object into a JSON string, whereas JSON.parse() will deserialize a JSON string into a JS object.