DEV Community

Cover image for Uses of JSON.parse() and JSON.stringify()
Md. Mizanur Rahaman
Md. Mizanur Rahaman

Posted on

Uses of JSON.parse() and JSON.stringify()

Sometimes we confused that when I have to use JSON.parse() and when JSON.stringify. But most of the time we have to use both of these.

Let's talk about these important topics-

JSON.parse()
JSON.parse() takes JSON string and transform it into JavaScript object.

Input

let userStringData = '{"name":"Hamid","email":"hamid@gmail.com","profession":"Doctor"}';

let userObjData = JSON.parse(userStringData);

console.log(userObjData);
Enter fullscreen mode Exit fullscreen mode

Output

{ name: 'Hamid', email: 'hamid@gmail.com', profession: 'Doctor' }
Enter fullscreen mode Exit fullscreen mode

JSON.stringify()
JSON.stringify() is total oposite of JSON.parse(). Where JSON.stringify takes JavaScript object and transform it into JSON string.

Input

let userObjectData = 
{ name: 'Hamid', email: 'hamid@gmail.com', profession: 'Doctor' }

let userJSONData = JSON.stringify(userObjectData);

console.log(userJSONData);
Enter fullscreen mode Exit fullscreen mode

Output

{"name":"Hamid","email":"hamid@gmail.com","profession":"Doctor"}
Enter fullscreen mode Exit fullscreen mode

Follow me on LinkedIn

Discussion (0)