jsonjavascriptjson-stringify

JSON.stringify() in JavaScript — Complete Guide with Examples

·9 min read·JSON Concepts

What Does JSON.stringify() Do?

JSON.stringify() converts a JavaScript value — an object, array, string, number, or boolean — into a JSON string. It is the counterpart to JSON.parse(), which does the reverse. Any time you send data to an API, save it to localStorage, or write it to a file, you serialize it with JSON.stringify() first.

javascript
const user = { name: "Ada", age: 36, active: true };
const json = JSON.stringify(user);
console.log(json); // => {"name":"Ada","age":36,"active":true}

The output is a single, minified line with no spaces. To read it, paste the result into the JSON Formatter or pretty-print it directly (see below).

JSON.stringify Syntax

javascript
JSON.stringify(value, replacer, space)
ArgumentTypePurpose
valueanyThe value to serialize
replacerfunction or arrayFilter or transform values (optional)
spacenumber or stringIndentation for pretty-printing (optional)

Pretty-Printing JSON (the space argument)

Pass a number as the third argument to indent the output. Two spaces is the most common convention:

javascript
const data = { name: "Ada", roles: ["admin", "dev"] };

JSON.stringify(data, null, 2);
// {
//   "name": "Ada",
//   "roles": [
//     "admin",
//     "dev"
//   ]
// }

JSON.stringify(data, null, "\\t"); // indent with tabs instead

The replacer Argument

The second argument controls which properties are included. Pass an array of keys to allowlist properties:

javascript
const user = { id: 1, name: "Ada", password: "secret" };

// Only keep id and name
JSON.stringify(user, ["id", "name"]);
// => {"id":1,"name":"Ada"}

Or pass a function to transform every value as it is serialized — perfect for redacting sensitive fields:

javascript
const safe = JSON.stringify(user, (key, value) =>
  key === "password" ? undefined : value
);
// => {"id":1,"name":"Ada"}

Returning undefined from the replacer drops that key from the output.

How JSON.stringify Handles Special Values

This is where most bugs come from. JSON only supports six data types, so JavaScript-only values are converted or dropped:

InputResult
undefined (in object)key is removed
undefined (in array)becomes null
Functionremoved (object) / null (array)
NaN, Infinitybecome null
Dateconverted to an ISO string
BigIntthrows a TypeError
Symbolremoved
javascript
JSON.stringify({ a: undefined, b: () => {}, c: NaN, d: new Date(0) });
// => {"c":null,"d":"1970-01-01T00:00:00.000Z"}

Custom Serialization with toJSON()

If an object has a toJSON() method, JSON.stringify() calls it and serializes the return value instead. This is exactly how Date produces an ISO string, and you can use it on your own classes:

javascript
class Money {
  constructor(cents) { this.cents = cents; }
  toJSON() { return (this.cents / 100).toFixed(2); }
}

JSON.stringify({ price: new Money(1999) });
// => {"price":"19.99"}

Common Pitfalls

Circular references throw. If an object references itself, stringify fails with TypeError: Converting circular structure to JSON. Break the cycle or use a replacer that tracks seen objects. See our guide on the circular structure error.

Dates become strings, not Dates. After a round trip through stringify then parse, date fields are strings. Revive them with a reviver in JSON.parse.

BigInt is not supported. Convert to a string first before serializing.

Frequently Asked Questions

How do I pretty-print JSON in JavaScript? Use JSON.stringify(value, null, 2) for two-space indentation. Pass a tab character for tab indentation.

How do I exclude a property from JSON.stringify? Return undefined for that key from a replacer function, or pass an array of the keys you want to keep.

Why is my undefined value missing after stringify? That is expected — undefined object properties are silently removed. Use null if you need the key to appear.

Once you have your JSON string, format it, minify it, or validate it. To read JSON back into an object, see how to parse JSON in JavaScript.

Try JSONKit — JSON Developer Tools

Format, validate, convert and diff JSON. Instant, accurate and 100% private.