Pretty-print JSON in the JavaScript console

How to pretty-print JSON in the JavaScript console, what JSON.stringify changes and drops, and when compact output or a plain object log works better.

If you want readable JSON in the JavaScript console, the standard approach is to pass an indentation value to JSON.stringify and log the result. It works in every modern browser and in Node.js without extra libraries. It also converts your data to a JSON string, and that conversion can change or drop values in ways that are easy to miss. This review covers what the technique changes, where it falls short, and when another console method or compact output is the better choice.

Is pretty-printed JSON right for your use?

Pretty-printing is a good fit when you need a static, readable snapshot of data you can copy into a bug report, a test fixture or a code review. Its main benefit is that the output is plain text showing the exact JSON a serializer would produce, with one property per line and consistent indentation.

The main tradeoff is fidelity. JSON.stringify only represents what JSON can express. Undefined values, functions, Maps, Sets, BigInts and circular references are dropped, altered or rejected. If you are debugging a live JavaScript object, logging the object directly usually shows more. If the output goes to a log pipeline or any tool that reads line by line, compact single-line JSON is usually the better choice.

What pretty-printing actually changes

This is a formatting transformation only. It does not reverse characters, substitute Unicode lookalikes or apply visual styling, and "pretty" here has nothing to do with decorative fonts. The serializer adds line breaks and indentation between JSON tokens. JSON treats that whitespace as insignificant, so parsing the pretty-printed string gives the same value as parsing the compact version. Whitespace inside string values is left as it was.

const data = { id: 7, tags: ["a", "b"], active: true };

console.log(JSON.stringify(data, null, 2));

The second argument is a replacer, and null means no filtering. The third argument sets the indentation. A number from 1 to 10 adds that many spaces per level, with larger numbers capped at 10. A string such as "\t" is used as the indent directly, and only its first 10 characters count. The example produces:

{
  "id": 7,
  "tags": [
    "a",
    "b"
  ],
  "active": true
}

Array elements each get their own line, which can make long arrays of short values take up many lines. Keys are not sorted. They follow the object's property order, where integer-like keys come first in ascending order and the rest follow in insertion order.

Console approaches compared

Approach | What you see | Key caveat
console.log(obj) | Interactive, expandable tree in browser DevTools; formatted inspection text in Node.js | In browsers the tree is a live reference, so expanding it later may show values that changed after the log call
console.log(JSON.stringify(obj, null, 2)) | Static indented text | Only JSON-representable data survives
console.dir(obj, { depth: null }) in Node.js | Full nested inspection output | Uses Node's inspection format, not valid JSON
console.table(arrayOfObjects) | Rows and columns | Suited to flat, uniform records, not deep nesting

Where meaning can quietly change

Because the output is JSON rather than a view of the JavaScript object, the following conversions happen without any warning:

  • Properties whose value is undefined, a function or a symbol are left out of objects.
  • The same values inside arrays become null, which keeps array positions intact.
  • NaN, Infinity and -Infinity become null.
  • Date objects become ISO 8601 strings through their toJSON method, so the type information is lost.
  • Map and Set instances become empty objects, {}, unless you convert them first.
  • BigInt values throw a TypeError.
  • Circular references throw a TypeError.
JSON.stringify({ a: undefined, b: () => 1, c: NaN, d: new Date(0), e: new Map([[1, 2]]) });
// '{"c":null,"d":"1970-01-01T00:00:00.000Z","e":{}}'

JSON.stringify([undefined]);
// '[null]'

A replacer function can handle some of these cases, but it creates its own change in meaning. The example below makes BigInts printable by turning them into strings. A reader of the output can no longer tell an original string from a converted number.

JSON.stringify(
  data,
  (key, value) => (typeof value === "bigint" ? value.toString() : value),
  2
);

For Maps, convert explicitly before logging, for example with Object.fromEntries(map) when the keys are strings. For circular structures, either log the object directly or use a replacer that tracks objects it has already seen.

Node.js and nesting depth

In Node.js, console.log on an object uses util.inspect with a default depth of 2. Anything nested deeper appears as [Object] or [Array]. This often leads people to reach for JSON.stringify when the real problem is the depth limit. If you need full detail and valid JSON is not required, remove the limit:

console.dir(obj, { depth: null });

// or, to get a string
const util = require("node:util");
console.log(util.inspect(obj, { depth: null, colors: true }));

This output keeps details that JSON drops. Maps, Sets, class names and circular markers all stay visible. It is not valid JSON, though, so do not paste it into a parser or a fixture file.

Does the result stay readable after copying or sharing?

Stringified output is ordinary text, so copying it from the console gives you what you saw. That is its practical advantage over an expanded object tree, which can copy with display artifacts or collapsed sections. Chromium-based DevTools and Firefox both document a console utility, copy(value), that places a value on the clipboard. Exact formatting can vary by browser and version, so check the result before relying on it.

Stringifying also solves a timing problem. A browser console tree is evaluated when you expand it, not when it was logged, so it can show later values. A JSON string is fixed at the moment of the log call, which makes it more reliable evidence when you are tracking down state changes.

Some destinations can damage indentation. Rich-text editors, chat tools and email clients may collapse leading spaces or change tabs unless the text is placed in a code block or preformatted area. No destination was tested for this review. Check the specific tool you plan to use, and when in doubt, share the JSON as an attached file or inside a code block.

For accessibility, indentation shows structure visually, but screen readers may announce or skip whitespace depending on the reader and its settings. No assistive technology was tested here. If the structure matters to someone using a screen reader, a short prose summary or a table of the key fields may serve them better than a long indented block.

For search and editing, multi-line output breaks tools that treat each line as a record. A grep for one field returns a single line without the rest of its object, and log aggregators that expect one JSON object per line may parse it incorrectly. Pretty-printed text is easy to edit by hand, but it should be checked with JSON.parse before reuse.

When plain or compact output is the better choice

  • Production or structured logs: use JSON.stringify(obj) with no indentation, so each record stays on one line.
  • Debugging live objects that contain functions, class instances, Maps, Sets or cycles: log the object directly, or use console.dir in Node.js.
  • Large payloads: indentation adds bytes and lines, so consider logging only the relevant subset.
  • Tabular records: console.table is often easier to scan than nested braces.
  • Sharing with people who need the meaning rather than the raw data: a brief plain-text summary may be clearer.

Evidence gaps

The serialization behavior described above follows the JSON.stringify specification and Node's documented util.inspect defaults. Clipboard formatting, the effect of pasting into particular chat or document tools, and screen reader handling of indented text were not tested for this review. They can vary by browser, version and settings, so verify them in your own environment before depending on them.