JavaScript (ES2026) reference · JSON and Global Functions
JavaScript JSON.stringify() Method
Convert a JavaScript value to a JSON string, optionally filtering keys and pretty-printing with indentation.
JavaScript (ES2026) Since ES5
Syntax
JSON.stringify(value, replacer, space)
Parameters
| Parameter | Description |
|---|---|
value |
The value to convert. |
replacer optional |
A function (key, value) or an array of keys to include. |
space optional |
Indentation: a number of spaces or a string. |
Return value
A JSON string, or undefined for values like functions.
Examples
const order = { id: 7, items: ["tea", "cake"], paid: true };
console.log(JSON.stringify(order));
console.log(JSON.stringify(order, null, 2));
Output
{"id":7,"items":["tea","cake"],"paid":true}
{
"id": 7,
"items": [
"tea",
"cake"
],
"paid": true
}const user = { name: "Ada", password: "secret", age: 36 };
console.log(JSON.stringify(user, ["name", "age"]));
console.log(JSON.stringify(user, (k, v) => (k === "password" ? undefined : v)));
Output
{"name":"Ada","age":36}
{"name":"Ada","age":36}console.log(JSON.stringify({ when: new Date(0), fn() {}, missing: undefined, nan: NaN, big: 10 }));
Output
{"when":"1970-01-01T00:00:00.000Z","nan":null,"big":10}Common mistakes
undefined, functions and symbols are skipped in objects (and becomenullin arrays).NaNandInfinitybecomenull;Dateobjects become ISO strings.- Circular references throw a
TypeError;BigIntvalues throw unless you convert them.
Version history
| Version | Change |
|---|---|
| JavaScript ES5 | Added |
Related
JSON.parse()· Turn a JSON string into a JavaScript value. Throws SyntaxError on invalid JSON; an optional reviver transforms values.- JavaScript JSON · tutorial