Docento.app
Office printer on a desk
All Posts

How to Format and Pretty-Print JSON (and When Not To)

By The Docento.app TeamPublished 4 min read
Try Docento's free PDF editor — No sign-up, 100% private — sign, annotate, and stamp PDFs in your browser.Open the editor

A minified JSON file is a single line of braces and commas that is nearly impossible to read. Pretty-printing re-indents it so the nesting is visible. It is a safe operation in most cases, but there are a few things to know first.

What pretty-printing changes

JSON ignores whitespace between tokens. These two documents mean exactly the same thing:

{"user":"ada","roles":["admin","editor"]}
{
  "user": "ada",
  "roles": [
    "admin",
    "editor"
  ]
}

Formatting adds line breaks and indentation. Minifying does the reverse and removes all optional whitespace to make the file smaller, which is useful for network transfer.

Ways to format JSON

In the browser. Open the file in Docento's Text & Markdown Editor and click Format JSON. The editor parses the text, then writes it back with two-space indentation. If the JSON is invalid, it will not format and the syntax bar points to the problem instead. Processing happens locally in your browser.

In a code editor. Most editors have a format-document command, usually keyed to a shortcut.

On the command line. Several tools can do it. python -m json.tool file.json is available wherever Python is installed, and jq . file.json is a popular option for people who work with JSON often.

In a script. In JavaScript, JSON.stringify(JSON.parse(text), null, 2) re-indents with two spaces. In Python, json.dumps(data, indent=2) does the same.

Choosing indentation

Two spaces is the most common convention for JSON, four spaces is also widespread, and tabs are less usual. Pick one for a project and stay consistent, ideally by setting a formatter rather than relying on habit. The choice does not affect meaning.

Sorting keys

Some formatters can sort object keys alphabetically. That gives stable output so that version-control diffs show real changes, not reorderings. JSON objects are formally unordered, but sorting can matter if another tool depends on the order, so do not enable sorting blindly on a file you do not own.

When formatting can lose information

Pretty-printing through a parser is safe for ordinary data, but be careful with:

  • Duplicate keys. {"a": 1, "a": 2} is allowed by the grammar but most parsers keep only the last value, so formatting silently drops the first.
  • Very large or very precise numbers. Parsers that read numbers as floating point change integers beyond 2^53 and trim precision. An ID such as 12345678901234567890 can come out altered.
  • Number formatting. 1.0 may be rewritten as 1, and 1e3 as 1000.
  • Comments and trailing commas. These are not valid JSON, so a strict formatter will refuse the file or drop the comments.
  • Key order. Most parsers preserve insertion order, but not all.

If the exact bytes matter, for example for a signed or hashed file, format a copy rather than the original.

Formatting very large files

A multi-megabyte file may be slow in a browser or editor because the whole file is loaded. The Docento editors cap text files at 5 MB for that reason. For larger files, use a command-line tool.

Minifying

To shrink a file, remove whitespace with the same tools. JSON.stringify without the indentation argument does this. Minifying a hand-edited config file that people still read is usually a mistake, so only do it for output meant for machines.

Format after fixing errors

Formatting is a good diagnostic: if a file refuses to format, it is invalid. Our guide to fixing invalid JSON covers the common causes.

Takeaway

Pretty-printing only changes whitespace and is safe for typical JSON. Take care with duplicate keys, huge numbers and files whose exact bytes matter, and use the Format JSON button in the online editor for a quick, private result.

Try Docento's free PDF editor

No sign-up, 100% private — sign, annotate, and stamp PDFs in your browser.

Open the editor

Related Posts