Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Timers JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS JSON JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Browser API JS Web API JS Graphics

Old Technologies

JS AJAX JS jQuery JS JSONP

JS Examples

JS Examples

JavaScript JSON Stringify

The JSON.stringify() Method

The JSON.stringify() method converts a JavaScript value into JSON text.

This way we can convert JavaScript variables into plain text before they can be:

  • Stored in a text file
  • Sent to a server over HTTP
  • Transmitted between applications

Note

The JSON.stringify() method returns a plain string.


Syntax

Syntax

JSON.stringify(value, replacer, space)
Parameter Description
value The JavaScript value to convert.
replacer An optional function or array that selects or transforms values.
space An optional number or string used to indent the JSON text.

Converting an Object

Example

Converting a JavaScript Object into JSON text.

const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const text = JSON.stringify(person);
Try it Yourself »

Converting an Array

Example

Converting a JavaScript Object into JSON text.

const cars = ["Ford", "Volvo", "BMW"];

const text = JSON.stringify(cars);
Try it Yourself »

Converting Other Values

JSON.stringify() can convert all JavaScript values that JSON supports.

JavaScript Value JSON Text
String "John"
Number 42
Boolean true
null null
Object {"name":"John","age":30,"city":"New York"}
Array ["Ford","Volvo","BMW"]

Example

JSON.stringify("John");
JSON.stringify(42);
JSON.stringify(true);
JSON.stringify(null);
Try it Yourself »


Selecting Properties

A replacer array can choose which properties to include.

Example

const person = {
  name: "John",
  age: 30,
  city: "New York"
};

let text = JSON.stringify(person, ["name", "city"]);
Try it Yourself »

The resulting JSON contains only the selected properties.


Transforming Values

A replacer function can modify values before they are converted.

Example

const person = {
  name: "John",
  age: 30
};

const text = JSON.stringify(person, function(key, value) {
  if (key == "age") {
    return value + 1;
  }
  return value;
});
Try it Yourself »

Formatting JSON

The optional space parameter makes JSON easier to read.

Example

const person = {
  name: "John",
  age: 30,
  city: "New York"
};

let text = JSON.stringify(person, null, 1);
Try it Yourself »

The value null selects NO replacer.

The value 1 indents each level with one space.


Unsupported Values

Some JavaScript values cannot be represented in JSON.

Value Result
undefined Omitted from objects. Converted to null in arrays.
Functions Omitted from objects. Converted to null in arrays.
Symbol Omitted from objects. Converted to null in arrays.
BigInt Throws a TypeError.

Example

const person = {
  name: "John",
  greet: function() {},
  age: undefined
};

document.getElementById("demo").innerHTML = JSON.stringify(person);
Try it Yourself »

Note

The resulting JSON syntax contains only the supported values.


Circular References

JSON.stringify() cannot convert objects that contain circular references.

Example

const person = {};

person.self = person;

JSON.stringify(person);

This throws a TypeError.


Common Stringify Mistakes

Stringifying JSON Twice

Calling JSON.stringify() twice adds escaped quotation marks.

Wrong

const person = {name: "John"};

const text = JSON.stringify(person);
const textAgain = JSON.stringify(text);

Correct

const person = {name: "John"};

const text = JSON.stringify(person);

Expecting Every Property to Appear

Properties containing undefined, functions, or symbols are omitted from objects.

Example

const person = {
  name: "John",
  age: undefined
};

const text = JSON.stringify(person);

The result is:

{"name":"John"}


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->