Blogchevron_rightJSON Tools
JSON Tools

JSON.stringify() Options: replacer, space, and toJSON()

Beyond basic usage, JSON.stringify() offers powerful options for filtering, transforming, and controlling JSON output. Here is the complete guide to all three parameters.

April 18, 2026·7 min read

The Array Replacer

When the replacer parameter is an array of strings, JSON.stringify includes only those keys in the output. JSON.stringify(user, ["id", "name", "email"]) produces JSON with only those three fields, regardless of how many properties the object has. This is a simple way to strip sensitive fields like passwords or tokens.

The array replacer applies recursively to nested objects, which may produce unexpected results. Only keys that match the allowlist appear at any nesting level. For objects with different schemas at different nesting levels, a function replacer gives you more precise control.

The Function Replacer

A function replacer is called for every key-value pair: JSON.stringify(obj, (key, value) => { if (key === "password") return undefined; return value; }). Returning undefined omits the key; returning a transformed value uses that instead. Returning the original value passes it through unchanged.

The function replacer is called with an empty string key for the root value first. Use this to handle the top-level object before processing individual properties. The context (this) inside the replacer is the object containing the current key, which lets you access sibling properties.

The space Parameter for Pretty Printing

The third parameter controls indentation. A number between 1 and 10 uses that many spaces per level. A string (up to 10 characters) is used as the indent. JSON.stringify(obj, null, "\t") uses tab indentation. JSON.stringify(obj, null, 2) uses 2-space indentation — the most common choice.

The space parameter does not affect the data content — only whitespace placement. Use it for logging, debugging output, and file storage where human readability matters. Never use it for network transmission where payload size should be minimized.

The toJSON() Method

Objects can define a toJSON() method that JSON.stringify calls during serialization. The return value of toJSON() is used instead of the object itself. This enables custom serialization without a replacer function. Date.prototype.toJSON() is why dates serialize as ISO strings.

For custom classes, implementing toJSON() gives you full control over the serialized representation: return { type: this.constructor.name, ...this.toPlainObject() }. This is cleaner than a replacer when the serialization logic belongs to the class rather than the caller.

Try JSON Stringify Online Free Online

No sign-up required. 100% client-side — your data never leaves your browser.

Open JSON Stringify Onlinearrow_forward

Frequently Asked Questions

Can I use both an array replacer and a space parameter?

Yes. JSON.stringify(obj, ["id", "name"], 2) produces pretty-printed JSON containing only the id and name fields. The replacer and space parameters are independent.

How do I serialize undefined values with JSON.stringify?

Undefined values are omitted by default. To include them, convert to null in a replacer: (key, value) => value === undefined ? null : value. This preserves the key with a null value.

Does the function replacer affect performance?

Yes, calling a function for every key-value pair adds overhead compared to plain JSON.stringify(). For large objects or performance-critical serialization, prefer the array replacer or avoid the replacer entirely.

JSON.stringify() Options: replacer, space, and toJSON()