Skip to content
Guides6 min read

JSON formatting explained: indentation, sorting and diffing

Why formatting matters even when machines don't care: readability, code review, source control, and reproducible builds.

By JSON Formatter Team

Formatting is for humans

Parsers don't care about whitespace. But you read JSON in code reviews, error logs and configuration files every day. Consistent formatting means fewer bugs and easier diffs.

Indentation

Pick one: 2 spaces or 4 spaces. Tabs are fine internally but many code review tools render them inconsistently. 2 spaces is the most common convention for JSON files in modern codebases.

Sort keys for reproducible builds

Alphabetically sorting object keys makes two JSON files with the same data byte-identical, which is invaluable for content-addressable caches, deterministic builds and stable diffs.

Trailing newlines

End every JSON file with a single newline character. It plays nicely with POSIX tools and git.

Minify only for the wire

Beautify JSON in your repo and in your logs. Minify only at the moment you send it over the network.