JSON is everywhere — API responses, config files, log lines, infrastructure manifests. Most of the time it arrives in a form that's optimized for transfer, not for reading. One long line, no whitespace, nested objects crammed together. Formatting (also called beautifying or pretty-printing) is the simple act of adding indentation and line breaks so a person can actually parse it with their eyes.
It sounds trivial, and the mechanics are. But getting into the habit of formatting JSON at the right moments saves a surprising amount of debugging time.
What formatting actually does
JSON has two valid forms for the same data: compact and expanded. Both parse identically. The difference is purely whitespace.
Compact (minified):
{"id":42,"name":"widget","tags":["a","b"],"meta":{"active":true,"score":9.5}}Formatted with two-space indentation:
{
"id": 42,
"name": "widget",
"tags": [
"a",
"b"
],
"meta": {
"active": true,
"score": 9.5
}
}Same object. The second version is the one you want when you're trying to figure out why your API is returning the wrong thing.
When you'd reach for it
A few recurring situations where formatting earns its keep:
- Inspecting an API response. You curl an endpoint, get a single 4KB line back, and need to find one field. Formatting turns that into something scannable.
- Reviewing config diffs. Git diffs on minified JSON are useless — a one-character change shows the whole line as modified. Formatted JSON produces clean, line-by-line diffs.
- Debugging log output. Structured logs are often JSON. Pulling a line out and expanding it makes nested error context readable.
- Writing documentation or test fixtures. Examples you paste into a README or a test file should be legible to the next person.
The flip side: when JSON is going over the wire or into a database, you usually want it minified to cut payload size. Format for humans, minify for machines.
How to do it
You've got options depending on where you are.
On the command line
If you have python installed, this works anywhere:
echo '{"a":1,"b":[2,3]}' | python -m json.tooljq is the heavier-duty choice and does far more than formatting:
curl -s https://api.example.com/thing | jq '.'Node works too, in a pinch:
node -e 'console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 2))' '{"a":1}'In code
Every mainstream language has a one-liner. In JavaScript the third argument to JSON.stringify controls indentation:
JSON.stringify(data, null, 2);Python's json.dumps takes an indent parameter the same way:
json.dumps(data, indent=2)In the browser
When you just want to paste something and read it without opening a terminal, a web tool is faster. Our JSON Beautifier handles that — paste in a blob, get formatted output, and it'll also flag syntax errors so you know immediately if the JSON is actually broken.
Common gotchas
Formatting itself rarely goes wrong. What goes wrong is the JSON you're trying to format isn't valid JSON in the first place. A few things bite people repeatedly:
- Trailing commas.
{"a": 1,}is valid in JavaScript object literals but not in JSON. This is probably the single most common cause of a "why won't this parse" moment. - Single quotes. JSON requires double quotes for both keys and string values.
{'a': 1}will fail. - Unquoted keys.
{a: 1}is a JS object, not JSON. Keys must be quoted strings. - Comments. Standard JSON doesn't support
//or/* */. Formats like JSONC and JSON5 do, but a strict parser will choke. - Not-a-number values.
NaN,Infinity, andundefinedare not valid JSON. If your serializer emits them, downstream parsers break. - Encoding and BOM. A byte-order mark at the start of a file will make some parsers fail on an otherwise perfect document. If a file "looks fine" but won't parse, check for a hidden BOM.
One subtle point: formatting is purely cosmetic and should never change your data. If you format a number like 9.50 and it comes back as 9.5, that's the parser normalizing the value, which is technically lossless for the number but can matter if you cared about the original text. For anything where the exact byte representation matters — signed payloads, checksums — don't round-trip through a formatter.
A short worked example
Say an endpoint hands you this and you need to confirm whether the user is an admin:
{"user":{"id":7,"roles":["editor","admin"],"settings":{"theme":"dark","notifications":{"email":true,"sms":false}}},"ts":1699999999}Hunting for admin in that line is annoying. Format it:
{
"user": {
"id": 7,
"roles": [
"editor",
"admin"
],
"settings": {
"theme": "dark",
"notifications": {
"email": true,
"sms": false
}
}
},
"ts": 1699999999
}Now the structure is obvious. roles is an array containing admin, the nesting of settings.notifications is clear, and if you were debugging why email notifications fired but SMS didn't, the answer jumps out.
A few habits worth keeping
- Pick an indentation and stick with it. Two spaces is the de facto standard for JSON and keeps files narrow enough to diff comfortably.
- Keep stored and transmitted JSON minified; format only when a human is looking.
- When something won't parse, format it first — the error location a formatter reports usually points you straight at the problem.
- Don't hand-edit deeply nested JSON. Expand it, make your change, let the tool re-indent.
Formatting JSON is the kind of tiny workflow improvement that pays off every single day. It costs nothing, catches a whole class of syntax errors early, and turns an unreadable wall of text into something you can reason about. Whether you do it with jq, a language built-in, or a browser tool, make it reflexive.
Photo by Chris Ried on Unsplash