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.
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
JSON.stringify(value, replacer, space)| Argument | Type | Purpose |
|---|---|---|
value | any | The value to serialize |
replacer | function or array | Filter or transform values (optional) |
space | number or string | Indentation 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:
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 insteadThe replacer Argument
The second argument controls which properties are included. Pass an array of keys to allowlist properties:
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:
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:
| Input | Result |
|---|---|
undefined (in object) | key is removed |
undefined (in array) | becomes null |
| Function | removed (object) / null (array) |
NaN, Infinity | become null |
Date | converted to an ISO string |
BigInt | throws a TypeError |
Symbol | removed |
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:
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.