JSON is everywhere — API responses, config files, log lines, infrastructure definitions. Most of the time it flows through your system without you ever looking at it. But the moment something breaks, you're staring at a single-line blob of 40,000 characters trying to figure out why your request failed. That's when formatting matters.
What "formatting JSON" actually means
Formatting (or beautifying, or pretty-printing) JSON means taking valid JSON and re-rendering it with consistent indentation and line breaks so a human can read it. The data doesn't change — only the whitespace does. The reverse operation, minifying, strips all that whitespace out to save bytes over the wire.
So these two documents are semantically identical:
{"user":{"id":42,"name":"Ada","roles":["admin","editor"]}}and
{
"user": {
"id": 42,
"name": "Ada",
"roles": [
"admin",
"editor"
]
}
}A machine couldn't care less. Your brain, however, reads the second one in a glance.
When you actually reach for it
A few recurring situations where I format JSON almost reflexively:
- Debugging API responses. curl and most HTTP clients hand you compact JSON. Pretty-printing it is the fastest way to confirm a field is missing or nested one level deeper than you expected.
- Reviewing config files. A diff on a minified JSON file is useless — one changed value shows the whole line as modified. Formatted JSON produces clean, line-by-line diffs.
- Writing documentation. Example payloads in a README or API doc should be readable, full stop.
- Inspecting log output. Structured logs are often one JSON object per line. Expand one and the shape becomes obvious.
And when you reach for it in the other direction: before sending JSON over the network, or storing it in a database column, minify it. Whitespace is dead weight in transit.
In code
Every mainstream language can do this. In JavaScript:
JSON.stringify(obj, null, 2); // 2-space indent
JSON.stringify(obj); // minifiedIn Python:
import json
json.dumps(obj, indent=2) // formatted
json.dumps(obj, separators=(',', ':')) // minifiedOn the command line, jq . or python -m json.tool will format piped input. All fine. But when you've got a payload pasted from Slack and just want to eyeball it, a browser-based JSON Beautifier is usually faster than wiring up a terminal command — paste, read, done.
The gotchas that bite
Formatting itself is trivial. The problems come from JSON that isn't quite valid, or from tools that silently change meaning.
1. It's not JSON until it parses
A formatter has to parse the input first. If your input has a trailing comma, a single-quoted key, or a comment, strict JSON parsers will reject it outright:
{
'name': 'Ada', // not valid JSON
"active": true, // trailing comma below
}That snippet is JavaScript object literal syntax, not JSON. JSON requires double-quoted keys, no comments, and no trailing commas. If your formatter throws an error, read the message — it usually points at the exact offending character.
2. Number precision
JSON numbers are just numbers, but JavaScript parses them as 64-bit floats. A large integer ID like 9007199254740993 can silently lose precision if it round-trips through JSON.parse / JSON.stringify. If you're dealing with big IDs, keep them as strings on the wire. A pure formatting tool that works on the text — rather than parsing to native numbers and back — won't corrupt them.
3. Key order
The JSON spec says objects are unordered. Most parsers preserve insertion order in practice, but don't rely on it for anything that matters. Some formatters offer an alpha-sort option, which is great for making two configs diff-friendly — just know it's reordering your keys.
4. Unicode and escaping
Characters like é or emoji are valid in JSON strings. Some formatters escape them to \uXXXX, others leave them as literal UTF-8. Both are correct, but if you're comparing two files, inconsistent escaping shows up as spurious diffs.
5. Pasting secrets
Be mindful of what you paste into any online tool. If a payload contains API keys, tokens, or personal data, use a local tool or scrub the sensitive fields first.
A short worked example
Say an endpoint returns this and you can't see why the frontend shows no products:
{"status":"ok","data":{"products":[],"meta":{"total":128,"page":1}},"errors":null}Format it:
{
"status": "ok",
"data": {
"products": [],
"meta": {
"total": 128,
"page": 1
}
},
"errors": null
}Now the bug jumps out: products is an empty array, but meta.total says 128. The data layer thinks there are 128 products; the serialiser isn't including them. The response is fine structurally — the problem is upstream. In the minified version you might have scanned right past the empty []. Indented, the mismatch is obvious.
That's the whole value proposition. Formatting doesn't fix anything, but it makes the shape of your data legible, and legible data is debuggable data.
A sensible default
Pick one indentation style and stick with it across your team — two spaces is the most common convention and keeps nesting readable without pushing deep structures off the right edge of the screen. Configure your editor to format JSON on save for files in the repo, and keep a beautifier handy for the throwaway payloads you're just inspecting.
None of this is complicated. The trick is making it a reflex: see a wall of JSON, format it before you try to reason about it. You'll waste a lot less time chasing bugs that were sitting in plain sight the whole time.
Photo by Florian Olivo on Unsplash