use JSON.parse() and JSON.stringify() in JavaScript

How to use JSON.parse() and JSON.stringfy() in JavaScript

JSON.parse()

Its convert JSON String to JavaScript Object. Its help to convert text into JavaScript Object. Use of this method convert JSON string into object and access single values using the dot. So at last its helps to convert JSON String to JS Object. When receiving data from a web server, the data is always a string and parse method help to convert in object.

Syntax
JSON.parse(text, reviver);

Second parameter use in JSON.parse its called reviver. It is a function that checks earh property in JSON, before return value.

Example

var json='{"language":"JavaScript","IDE":"Visual Studio Code","theme":"dark"}';
var obj=JSON.parse(json);
console.log(obj);

Result
{ language: 'JavaScript', IDE: 'Visual Studio Code', theme: 'dark' }

JSON.stringify()

When user need to send data to web server the data should into string format so convert JavaScript Object into string with using JSON.stringify.

Its help to convert a object to a string that contain object key value pair and string are store all data. Many times data need to send in string format for stroing into database or send to web server on that time require to convert into string.

var value = { name: "Logan", age: 21, location: "London" };
var result = JSON.stringify(value);

Output:
{"name":"Logan", "age":21, "location":"London"}