Tutorial

How To Use JSON.parse() and JSON.stringify()

Updated on November 24, 2021
authorauthor

Alligator.io and Haley Mills

English
How To Use JSON.parse() and JSON.stringify()

Introduction

The JSON object, available in all modern browsers, has two useful methods to deal with JSON-formatted content: parse and stringify.

JSON.parse()

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

let userStr = '{"name":"Sammy","email":"sammy@example.com","plan":"Pro"}';

let userObj = JSON.parse(userStr);

console.log(userObj);

Executing this code will produce the following output:

Output
{name: 'Sammy', email: 'sammy@example.com', plan: 'Pro'} email: "sammy@example.com" name: "Sammy" plan: "Pro"

Trailing commas are not valid in JSON, so JSON.parse() throws an error if the string passed to it has trailing commas.

JSON.parse() can take a function as a second argument that can transform the object values before they are returned.

Here the object’s values are transformed to uppercase in the returned object of the parse method:

let userStr = '{"name":"Sammy","email":"sammy@example.com","plan":"Pro"}';

let userObj = JSON.parse(userStr, (key, value) => {
  if (typeof value === 'string') {
    return value.toUpperCase();
  }
  return value;
});

console.log(userObj);

Executing this code will produce the following output:

Output
{name: 'SAMMY', email: 'SAMMY@EXAMPLE.COM', plan: 'PRO'} email: "SAMMY@EXAMPLE.COM" name: "SAMMY" plan: "PRO"

The values have been transformed to uppercase characters.

JSON.stringify()

JSON.stringify() takes a JavaScript object and transforms it into a JSON string.

let userObj = {
  name: "Sammy",
  email: "sammy@example.com",
  plan: "Pro"
};

let userStr = JSON.stringify(userObj);

console.log(userStr);

Executing this code will produce the following output:

Output
{"name":"Sammy","email":"sammy@example.com","plan":"Pro"}

JSON.stringify() can take two additional arguments. The first one is a replacer function. The second is a String or Number value to use as a space in the returned string.

The replacer function can be used to filter out values, as any value returned as undefined will be out of the returned string:

let userObj = {
  name: "Sammy",
  email: "sammy@example.com",
  plan: "Pro"
};

function replacer(key, value) {
  console.log(typeof value);
  if (key === 'email') {
    return undefined;
  }
  return value;
}

let userStrReplacer = JSON.stringify(userObj, replacer);

console.log(userStrReplacer);

Executing this code will produce the following output:

Output
{"name":"Sammy","plan":"Pro"}

The email key-value pair has been removed from the object.

And an example with a space argument passed-in:

let userObj = {
  name: "Sammy",
  email: "sammy@example.com",
  plan: "Pro"
};

let userStrSpace = JSON.stringify(user, null, '...');

console.log(userStrSpace);

Executing this code will produce the following output:

Output
{ ..."name": "Sammy", ..."email": "sammy@example.com", ..."plan": "Pro" }

The indentation has been replaced with ....

Conclusion

In this tutorial, you used the JSON.parse() and JSON.stringify() methods. If you’d like to learn more about working with JSON in Javascript, check out our How To Work with JSON in JavaScript tutorial.

For more information on coding in JavaScript, take a look at our How To Code in JavaScript series, or check out our JavaScript topic page for exercises and programming projects.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Alligator.io

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Please make the following change to correct the “stringify” demonstration, example code: let userObj = { name: “Sammy”, email: “sammy@example.com”, plan: “Pro” };

let userStrSpace = JSON.stringify(userObj , null, ‘…’); //**note change here.

console.log(userStrSpace);

Thanks in advance, RL Glover

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more