Pretty Print JSON with JSON.stringify

How JSON.stringify's space argument pretty-prints JSON, what it changes and what it does not, and when compact output or a plain-text alternative is the better choice.

JavaScript's built-in JSON.stringify can produce indented, multi-line JSON through its third argument. This review covers what that formatting changes, where it helps, where it gets in the way, and when compact output is the better choice. In this context, "pretty" means whitespace layout. It does not mean decorative Unicode fonts, mirrored text or CSS styling.

Is pretty-printed JSON suitable for your use?

Pretty-printed output from JSON.stringify suits work that people read: debugging, logging during development, configuration files, documentation examples and code review diffs. The main benefit is that nesting becomes visible at a glance. The decisive tradeoff is that the extra whitespace adds size and spreads one value across many lines. Choose compact output (no space argument) when the JSON goes over a network, into storage, into a size-constrained field, or into a line-oriented format such as newline-delimited JSON, where each record must sit on a single line.

Pretty printing changes only the layout of the serialized text, but the serialization step itself can be lossy. Whether the result is suitable depends as much on what your data contains as on the indentation you pick.

What the space argument actually changes

The function signature is JSON.stringify(value, replacer, space). The space argument controls indentation:

  • A number sets how many spaces are used per nesting level. Values above 10 are clamped to 10, and values below 1 produce no indentation.
  • A string, such as a tab character, is used as the indent unit. Only its first 10 characters are used.
  • If space is omitted, null or an empty string, the output is compact, with no added whitespace.
  • The replacer argument is separate. It can be null, a function that transforms values, or an array of property names that limits which keys are included.

With indentation turned on, each object property and each array element goes on its own line, indented one level deeper than its parent. Empty objects and arrays still render as {} and []. A common two-space example:

const data = { name: "Ada", tags: ["math", "code"], active: true };
console.log(JSON.stringify(data, null, 2));

// Output:
{
  "name": "Ada",
  "tags": [
    "math",
    "code"
  ],
  "active": true
}

The added whitespace sits between tokens and never inside string values. A standard JSON parser therefore reads the pretty and compact forms as the same data. Calling JSON.parse on either form returns an equivalent value. String contents, including non-ASCII characters and multi-code-point emoji, are kept as written rather than reordered or restyled. Quotes, backslashes and control characters are escaped, as they are in compact output.

What serialization changes regardless of indentation

Before you use pretty output as a faithful view of a JavaScript value, keep in mind that JSON.stringify follows JSON's data model and not JavaScript's. The documented behaviors below apply with or without the space argument:

  • Object properties whose values are undefined, functions or symbols are omitted. In arrays, these values become null.
  • NaN, Infinity and -Infinity become null.
  • Date objects become ISO 8601 strings through their toJSON method, so the output no longer marks them as dates.
  • Map and Set instances serialize as {}, which loses their contents.
  • BigInt values throw a TypeError unless a replacer or toJSON method handles them.
  • Circular references throw a TypeError.
  • Key order follows the object's own property order. Keys are not sorted.
const value = { a: undefined, b: () => 1, c: NaN, d: new Date(0) };
console.log(JSON.stringify(value, null, 2));

// Output:
{
  "c": null,
  "d": "1970-01-01T00:00:00.000Z"
}

When pretty output is used to inspect state, these omissions can mislead. A missing key might mean the property was undefined, not absent. A null might have been NaN. For debugging rich values, a runtime's own inspection tools may show more than JSON does.

Readability and usability after copying or sharing

Pretty-printed JSON is ordinary text made of spaces or tabs and line breaks. It needs no special characters or styling, so it does not depend on font support the way decorative Unicode text does. Its readability still depends on the destination keeping whitespace intact:

  • Code editors, terminals and plain-text files generally keep the layout. A monospaced font keeps the indentation visually aligned.
  • In HTML, consecutive whitespace collapses by default. Indentation is lost unless the text sits inside a preformatted element or is styled to preserve whitespace.
  • Rich-text editors, chat apps and form fields vary. Some reflow lines, trim leading spaces or convert tabs. Wrapping the JSON in a code block, where the destination supports one, usually helps. Test any specific destination before relying on it.
  • Tab indentation can display at different widths in different tools. Space indentation renders the same wherever a monospaced font is used.

Even when a destination mangles the indentation, the JSON stays valid as long as characters inside strings are not altered. JSON parsers ignore whitespace between tokens. The risk comes from tools that replace straight quotes with typographic quotes or insert characters. That breaks parsing, not just layout.

For searching and editing, pretty output helps line-based diffs, because a change to one property usually shows up as one changed line. It hurts line-oriented tools that expect a whole record per line, since one object now spans many lines. Two objects with the same data but different key order also produce different diffs. Sorting keys first, with a replacer or a preprocessing step, makes diffs more stable.

On accessibility: indentation shows structure visually, and assistive technologies differ in whether and how they announce leading whitespace. That often depends on user settings. No tested evidence on specific screen readers was available for this review, so no claim is made about how any of them handles indented JSON. When the structure matters to a non-visual reader, the safer approach is to give a short plain-language summary of the data next to the JSON, without relying on indentation alone.

When compact or plain output is the better choice

  • Network payloads and stored records, where added whitespace only adds size.
  • Newline-delimited JSON logs and streams, which require one value per line.
  • Values embedded in places with length limits or strict formatting, such as query parameters, headers or single-line config fields.
  • Output checked by exact string comparison. Pretty and compact forms differ as strings even when they parse to equal values.

Gaps in this review

This review is based on documented JSON.stringify behavior and general text-handling principles. It includes no measured size overhead and no tests of specific editors, chat platforms or assistive technologies. Check behavior in the environments you actually use before relying on it.