Back to blog
Guides7 min read

How to Format JSON: Pretty-Print and Beautify Cleanly

A practical guide to formatting JSON: indentation choices, sorting keys, formatting vs minifying, doing it in code and in the browser, and why consistent formatting matters for diffs and reviews.

By Saifur Rahman Shochib

Formatting — also called beautifying or pretty-printing — turns a dense, single-line blob of JSON into something a human can actually read: indented, line-broken, and aligned. It is the first thing most developers do with an API response or a log payload. This guide covers how to format JSON well, the indentation and key-sorting choices that matter, and why consistent formatting pays off in code review and version control.

What formatting does

Formatting adds structured whitespace — newlines and indentation — so nesting becomes visible. A formatter parses the JSON, confirms it is valid, and re-serializes it with consistent indentation. The data is unchanged; only its presentation improves. This is the inverse of minification, which strips that whitespace away.

Minified (one line)json
{"name":"Ada","tags":["x","y"],"meta":{"active":true}}
Formatted (readable)json
{
  "name": "Ada",
  "tags": ["x", "y"],
  "meta": { "active": true }
}

Choosing an indentation style

  • Two spaces — the most common default, compact and readable; used by most JavaScript and web tooling.
  • Four spaces — more visual separation for deeply nested data, common in Python-adjacent codebases.
  • Tabs — let each developer control display width and can be friendlier for accessibility.
  • Pick one and apply it consistently across a project so diffs stay clean.

Sorting keys

Many formatters can alphabetically sort object keys. Sorting makes two documents directly comparable and produces stable, minimal diffs when files are regenerated — valuable for lockfiles, snapshots, and config you check into version control. The trade-off is that sorting discards the original author's intended ordering, so reserve it for machine-generated files rather than hand-curated ones where order carries meaning.

Format versus minify

Formatting and minifying are two directions of the same lossless transform. Format when you need to read, debug, review, or edit JSON. Minify when you need to ship or store it compactly. A healthy workflow keeps source files formatted and minifies only as a build or transmission step.

Formatting in code

In JavaScript, JSON.stringify(obj, null, 2) pretty-prints with two-space indentation. In Python, json.dumps(obj, indent=2) does the same, and adding sort_keys=True orders the keys. On the command line, jq . formats and colorizes JSON, and python -m json.tool is a quick built-in formatter. All of them parse first, so they reject invalid JSON instead of mangling it.

Pretty-print in JavaScriptjavascript
JSON.stringify(obj, null, 2); // 2-space indentation
Pretty-print in Pythonpython
json.dumps(obj, indent=2, sort_keys=True)

Formatting in the browser

For a quick one-off, an online formatter is faster than writing code — paste the JSON, choose your indentation, and copy the result. A client-side JSON Formatter does this entirely in your browser, so sensitive payloads never leave your machine, and it reports the exact location of any syntax error. For sustained editing rather than a single pass, a JSON Editor gives you syntax highlighting, inline validation, and format-on-demand in one place.

Why consistent formatting matters

Consistent formatting is not just cosmetic. When everyone uses the same indentation and, where appropriate, sorted keys, version-control diffs show only real changes instead of whitespace noise, code reviews get faster, and merge conflicts shrink. Standardizing formatting across a team removes a whole category of pointless churn.

Frequently asked questions

What is the difference between formatting and minifying JSON?

Formatting adds whitespace to make JSON readable; minifying removes whitespace to make it compact. Both are lossless and reversible — the underlying data is identical in either form.

What indentation should I use for JSON?

Two spaces is the most common default and works well for most projects. Use four spaces or tabs if your team prefers; the important thing is to apply one style consistently so diffs stay clean.

Should I sort JSON keys when formatting?

Sort keys for machine-generated files where you want stable diffs and easy comparison. Avoid sorting hand-edited files where the author's key order conveys meaning or grouping.

How do I format JSON in JavaScript?

Use JSON.stringify(obj, null, 2) for two-space pretty-printing. The third argument controls indentation; pass a number of spaces or a string such as a tab character.

Does formatting JSON change its meaning?

No. Formatting only changes whitespace and presentation. The parsed object is exactly the same, so formatted and minified versions of the same JSON are equivalent.

Related tools

Try these tools

Keep reading

Related guides