What is the Difference Between JSON.stringify and JSON.parse

The JSON object has two methods to deal with JSON-formatted content: parse() and stringify(). Let’s see what each of them does and what the major differences are between these two methods.

JSON.parse()

The JSON.parse() method takes a JSON string and transforms it into a JavaScript object. Parsing means to divide into grammatical parts and identify the parts and their relations to each other. After parsing the string from web server, it becomes a JavaScript object and then you can access the data. Here is an example of JSON.parse():

Javascript JSON parse method
let myObject = { key1: "some text", key2: true, key3: 8 }; let objectAsString = JSON.stringify(myObject); let objectAsStringAsObject = JSON.parse(objectAsString); console.log(objectAsStringAsObject); // {key1: "some text", key2: true, key3: 8} console.log(typeof(objectAsStringAsObject));// "object"

JSON.stringify()

The JSON.stringify() method takes two additional arguments, where the first one is a replacer function and the second one is a String or Number value to use as a space in the returned string:

Javascript JSON stringify method
let myObject = { key1: "some text", key2: true, key3: 8 }; let objectAsString = JSON.stringify(myObject); console.log(objectAsString);// "{"key1":"some text","key2":true,"key3":8}" console.log(typeof(objectAsString)); // "string"

As the web server uses strings, the data being send to the server has to be a string. So the JSON.stringify() method is designed to convert a JavaScript object into a string to send it to the server.