DEV Community

Lourdes Suello
Lourdes Suello

Posted on • Edited on

1

Everything you know about JSON.parse() and JSON.stringify()

JSON.stringify()

Transforms a Javascript object to a JSON string.
A common use of JSON is to exchange data to/from a web server.
When sending data to a web server, the data has to be a string.
Convert a JavaScript object into a string with JSON.stringify().

Example:
Imagine we have this object in JavaScript:

const student = {
    name: "Alex",
    age: 20,
    email: "alex@gmail.com"
};

const studentStr = JSON.stringify(student);
console.log(studentStr);

{ "name":"Alex", "age":20, "email":"alex@gmail.com"}
Stringify a JavaScript Array

It is also possible to stringify JavaScript arrays.

Example:
Imagine we have this object in JavaScript:

const arrName = [ "John", "Peter", "Sally", "Jane" ];

const jsonName = JSON.stringify(arrName);
console.log(jsonName);

["John","Peter","Sally","Jane"]

JSON.parse()

Takes a JSON string and transform it into a Javascript Object.

Example:
Imagine we received this text from a web server.
We will take the studentStr sample above.

const jsObject = JSON.stringify(studentStr);
console.log(jsObject);

Object { age: 20, email: "alex@gmail.com", name: "Alex" }

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay