JSON.stringify
JSON.stringify(value, replacer, space)Serialize a value to JSON text, applying the substitutions and omissions listed above.
| Parameter | Description |
|---|---|
value | The JavaScript value to encode. |
replacer | Optional function or array of keys that can change or filter what is encoded. |
space | Optional number or string used to insert white space into the output. Omit it for compact text. |
Returns: A JSON string, or undefined if the top-level value is undefined or a function.
Throws
TypeError— Thrown for BigInt values and for cyclic object graphs. JSON.parse does not throw this for invalid text.
- If you stringify a Date and later parse the result, you get a string, not a Date object.
- A replacer can encode values that the default serializer rejects, if you decide how they should appear as JSON.
console.log(JSON.stringify({ a: 1, b: undefined, c: null }));
console.log(JSON.stringify([1, undefined, null]));
console.log(JSON.stringify({ n: NaN, i: Infinity }));
console.log(JSON.stringify({ d: new Date("2026-11-22T00:00:00.000Z") }));
try { JSON.stringify({ x: 1n }); } catch (e) { console.log(e.name); }
const cyclic = {};
cyclic.self = cyclic;
try { JSON.stringify(cyclic); } catch (e) { console.log(e.name); }Expected output
{"a":1,"c":null}
[1,null,null]
{"n":null,"i":null}
{"d":"2026-11-22T00:00:00.000Z"}
TypeError
TypeErrorRelated: JSON Diff tool, JSON.parse