JSON is everywhere — API responses, config files, log lines, infrastructure state. Most of the time it arrives as a single dense line of text that's perfectly valid and completely unreadable. Formatting (or "beautifying") JSON is the act of adding consistent indentation and line breaks so a human can actually parse it with their eyes.
It sounds trivial. It mostly is. But there are enough real-world edge cases that a quick rundown is worth your time.
What formatting actually does
Formatting changes whitespace, nothing else. A well-behaved formatter will never alter the data: no reordering keys (unless you ask), no changing number precision, no dropping values. Take this response from an API:
{"id":42,"name":"Ada","roles":["admin","editor"],"active":true,"meta":{"lastLogin":"2024-01-15T09:30:00Z","loginCount":218}}After formatting with two-space indentation:
{
"id": 42,
"name": "Ada",
"roles": [
"admin",
"editor"
],
"active": true,
"meta": {
"lastLogin": "2024-01-15T09:30:00Z",
"loginCount": 218
}
}Same bytes of data, wildly different readability. The nesting is now obvious, you can see the shape of meta at a glance, and if there were a missing comma you'd spot it fast.
When you'd reach for it
A few situations where I format JSON almost reflexively:
- Debugging API responses. curl spits out a wall of text. Formatting it turns a guessing game into a quick read.
- Reviewing config files. A 300-line
package.jsonor Terraform output is far easier to diff and reason about when it's structured. - Writing test fixtures. Readable fixtures are maintainable fixtures. Nobody wants to edit a minified blob in a test file.
- Spotting structure bugs. Deeply nested data, unexpected arrays, or a stray extra wrapper object jump out once indented.
The flip side: you minify (strip whitespace) for the wire. Production API payloads and asset bundles should ship minified to save bytes. Format for humans, minify for machines.
Common gotchas
JSON is stricter than you remember
JSON is not JavaScript. These are all invalid and will break a strict formatter:
- Trailing commas:
{"a": 1,} - Single-quoted strings:
{'a': 1} - Unquoted keys:
{a: 1} - Comments:
// like this
People copy a snippet out of a JS source file, paste it into a formatter, and get a parse error. The usual culprit is one of the above. If you genuinely need comments, you're looking at JSON5 or JSONC, which are different formats with their own tooling.
Numbers and precision
Large integers are a classic footgun. JSON itself has no size limit on numbers, but many parsers (JavaScript's included) use 64-bit floats, so a value like 9007199254740993 can silently lose precision. A good formatter treats the number as text and doesn't touch it — but if you round-trip through a language's native parser, you might corrupt it. When IDs are big, treat them as strings end to end.
Encoding and BOMs
A byte-order mark at the start of a file, or a mismatched encoding, produces cryptic "unexpected token" errors on what looks like valid JSON. If a file refuses to parse and looks fine, check for a sneaky BOM or non-UTF-8 bytes.
Key order
JSON objects are technically unordered. Most formatters preserve insertion order because that's what people expect, but don't rely on order for correctness. If you want deterministic diffs, sort keys deliberately — just know that's a transformation, not pure formatting.
Indentation consistency
Two spaces, four spaces, tabs — pick one and let a formatter enforce it. Mixed indentation in a hand-edited config is a reliable source of noisy diffs and merge conflicts.
A worked example
Say you hit an endpoint and get this back on one line:
{"status":"ok","data":{"users":[{"id":1,"name":"Sam","tags":[]},{"id":2,"name":"Jo","tags":["beta"]}],"page":1,"total":2}}You suspect the tags field is sometimes missing. Dropping it into a formatter gives you:
{
"status": "ok",
"data": {
"users": [
{
"id": 1,
"name": "Sam",
"tags": []
},
{
"id": 2,
"name": "Jo",
"tags": [
"beta"
]
}
],
"page": 1,
"total": 2
}
}Now it's obvious: user 1 has an empty array, user 2 has one tag. Your frontend code assuming tags is always present is fine here — but if a third user came back without the key at all, the formatted view makes that gap impossible to miss.
For quick one-offs, paste the raw string into a browser-based tool like Devsly's JSON Beautifier and read the result. It also catches syntax errors, which saves you squinting at a terminal trying to find the misplaced bracket.
Doing it in code
When you want formatting as part of a script or build step, every major language has it built in. In JavaScript:
const formatted = JSON.stringify(data, null, 2);
console.log(formatted);The third argument is the indent — a number of spaces or a string like "\t". In Python:
import json
print(json.dumps(data, indent=2))On the command line, jq . is the workhorse:
curl -s https://api.example.com/users | jq .And to go the other way — minify for shipping:
cat config.json | jq -c .A quick mental model
Think of formatting as a lens, not an edit. The JSON is the same before and after; you're just changing how you view it. That framing keeps you honest about what's safe (whitespace) versus what's a real transformation (sorting keys, re-encoding numbers, stripping comments).
Keep a formatter close — in your editor, in your shell, or a browser tab. The time you save hunting for a missing comma across a thousand-character line adds up fast, and well-formatted fixtures and configs pay dividends every time a teammate opens the file.
Photo by Florian Olivo on Unsplash