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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs