Blogchevron_rightJSON Tools
JSON Tools

JSON Pretty Print in JavaScript: JSON.stringify() Explained

JavaScript's JSON.stringify() is the built-in way to pretty print JSON. Understanding its space parameter and replacer unlocks powerful formatting capabilities.

April 18, 2026·7 min read

The space Parameter in JSON.stringify

JSON.stringify(value, replacer, space) accepts a third argument that controls indentation. When space is a number, JSON.stringify uses that many spaces for each indentation level. When space is a string, that string is used for indentation. JSON.stringify(data, null, 2) is the most common invocation for pretty printing.

The maximum useful value for the numeric space parameter is 10 — JSON.stringify clamps values above 10 to 10. In practice, 2 or 4 are the only values worth using. A tab character passed as the space string ('\t') produces tab-indented output useful for certain editors or style guides.

Using the replacer to Filter Output

The second parameter — the replacer — can be an array of key names to include, or a function that transforms each value. To pretty print only specific fields, pass an array: JSON.stringify(obj, ["name", "email"], 2) produces pretty-printed JSON containing only the name and email keys.

A replacer function receives each key and value during serialization and can return the transformed value, or undefined to omit the key. This is powerful for removing sensitive fields (like passwords or tokens) from JSON before logging, or for converting non-serializable types like Dates to formatted strings.

Pretty Printing in Browser Console

When you pass an object to console.log() in a browser, the console renders it as an interactive, collapsible tree — its own form of pretty printing. You can expand and collapse nested objects and arrays to explore the structure without formatting the JSON as a string.

For sharing or copying formatted JSON from the console, use JSON.stringify(obj, null, 2) and copy the result. The string output is safe to paste into any text context and preserves the exact structure as a static snapshot rather than a live JavaScript reference.

Pretty Printing API Responses in JavaScript

When fetching API data, pretty printing is useful for debugging. After const data = await response.json(), use console.log(JSON.stringify(data, null, 2)) to log a formatted view of the response. This shows the full structure in a readable format rather than the collapsed [Object Object] that console.log(data) produces in some contexts.

For production logging, omit the space parameter or use JSON.stringify(data) for compact output. Reserve pretty printing for development and debugging environments where log readability matters more than log volume.

Try JSON Pretty Print Free Online

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

Open JSON Pretty Printarrow_forward

Frequently Asked Questions

Why does JSON.stringify return undefined for some values?

JSON.stringify returns undefined (not the string "undefined") for values that are not JSON-serializable: functions, Symbol, and undefined values are silently omitted from objects or replaced with null in arrays.

How do I pretty print JSON in Node.js?

Use JSON.stringify(data, null, 2) exactly as in the browser. To write pretty-printed JSON to a file, use fs.writeFileSync("output.json", JSON.stringify(data, null, 2)).

Is there a performance cost to pretty printing?

Yes. Pretty printing is slower than minified serialization and produces larger strings. For performance-critical paths, use JSON.stringify(data) without the space parameter.

JSON Pretty Print in JavaScript: JSON.stringify() Explained