Skip to content

Stringify ( ) - Optional Parameters

JSON.stringify(value, replacer, space) turns a value into a JSON string. Most code passes only the value, but the two optional parameters are useful. The replacer chooses which properties are written or changes their values, and the space parameter indents the output so that people can read it.

With one argument, every property whose value JSON can hold is written, with no spaces.

const book = { title: 'Middlemarch', author: 'George Eliot', year: 1871, inStock: true };
console.log(JSON.stringify(book));

This prints:

{"title":"Middlemarch","author":"George Eliot","year":1871,"inStock":true}

An array of property names acts as an allow-list, so only those properties are written. The list applies at every level of nesting, not just the top, which catches people out.

const order = { id: 17, customer: { name: 'Priya', email: 'priya@example.com' }, total: 42.5 };
console.log(JSON.stringify(order, ['id', 'total']));
console.log(JSON.stringify(order, ['id', 'customer']));
console.log(JSON.stringify(order, ['id', 'customer', 'name']));

This prints:

{"id":17,"total":42.5}
{"id":17,"customer":{}}
{"id":17,"customer":{"name":"Priya"}}

Naming customer alone gives an empty object, because none of its own properties are on the list. To keep a nested property, its name has to be listed as well.

A replacer function is called with each key and value, and its return value is written in place of the original. Returning undefined leaves the property out. This is a simple way to keep secrets out of a log.

const user = {
name: 'Priya',
password: 'correct-horse',
lastLogin: new Date(Date.UTC(2026, 11, 1, 9, 30)),
loginCount: 12,
};
const json = JSON.stringify(user, (key, value) => {
if (key === 'password') return undefined;
return value;
});
console.log(json);

This prints:

{"name":"Priya","lastLogin":"2026-12-01T09:30:00.000Z","loginCount":12}

The function is called first for the whole value, with an empty string as the key, and then for each property and array element on the way down. Logging the calls shows the order.

JSON.stringify({ size: 'large', toppings: ['cheese'] }, (key, value) => {
console.log(JSON.stringify(key), value);
return value;
});

This prints:

"" { size: 'large', toppings: [ 'cheese' ] }
"size" large
"toppings" [ 'cheese' ]
"0" cheese

A replacer that returns undefined for the empty key would therefore throw the whole value away, so a filter should test for the keys it means to drop rather than for the ones it keeps.

The third argument indents each level. A number gives that many spaces, up to 10. A string, such as '\t', is used as the indent itself, and only its first 10 characters count. Pass null as the replacer to keep every property.

const basket = { items: ['apples', 'bread'], total: 3.2 };
console.log(JSON.stringify(basket, null, 2));
console.log(JSON.stringify(basket, null, '--'));

This prints:

{
"items": [
"apples",
"bread"
],
"total": 3.2
}
{
--"items": [
----"apples",
----"bread"
--],
--"total": 3.2
}

JSON has no undefined, functions or symbols. Properties with those values are left out of objects, and the same values inside an array become null, so that the positions of the other elements stay the same. NaN and Infinity also become null.

const record = {
name: 'Sam',
nickname: undefined,
greet() {},
id: Symbol('id'),
scores: [1, undefined, () => {}],
average: NaN,
};
console.log(JSON.stringify(record));

This prints:

{"name":"Sam","scores":[1,null,null],"average":null}

A BigInt value throws a TypeError instead, and so does an object that refers back to itself.

An object with a toJSON() method is written as whatever that method returns. Date has one built in, which is why lastLogin came out as an ISO date string in the replacer example.

const temperature = {
celsius: 21,
toJSON() {
return `${this.celsius}°C`;
},
};
console.log(JSON.stringify({ room: 'kitchen', temperature }));

This prints:

{"room":"kitchen","temperature":"21°C"}

MDN’s JSON.stringify page lists the remaining rules.

Steve Griffith covers this topic in his video JSON stringify method - the optional parameters.