// Tutorial //

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

Published on February 1, 2017 · Updated on November 24, 2021
Default avatar
By Alligator.io
Developer and author at DigitalOcean.
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.


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

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