Why Formatting JSON Properly Saves You Time
Why debugging unformatted JSON is harder than it needs to be.
Summary
In a JSON blob squeezed onto one line, a missing comma or bracket is nearly invisible. An indented, syntax-highlighted view makes structural errors visible at a glance.
Step by step
- Paste the raw JSON output into a formatter.
- Review the structure in the indented view.
- Find the part flagged as invalid.
- Copy the corrected JSON and use it.
Why it matters
Formatting isn't just cosmetic. In indented JSON you can see at a glance which field belongs to which object, and you don't lose your place in deeply nested data. When a team keeps shared config files consistently formatted, diffs in version control show only the lines that really changed. In a one-line file, even a tiny edit makes the whole line look different.
Pretty-print or minify?
The indented, readable form is for development and debugging. The minified form strips whitespace to make the file smaller, which is what you want for data sent over the network. Both carry exactly the same data. One is for people, the other for machines. A common routine is to edit the formatted version and minify only when shipping.
Typical errors a formatter exposes
- A trailing comma after the last item. JavaScript tolerates it, the JSON standard doesn't.
- Single quotes. JSON keys and string values need double quotes.
- Comments. Standard JSON has no comment syntax.
- Unquoted keys, or values like undefined and NaN that don't exist in JSON.
- An unescaped double quote or backslash inside a string.
A worked scenario
A developer is trying to work out why a payment service's response breaks his app. In the log, the response is a single line several thousand characters long. Pasted into a formatter, the structure becomes obvious: the "amount" field he expects isn't at the root, it's nested inside a "transaction" object. The JSON was valid all along. His code was simply looking at the wrong level. Spotting that inside one long line would have taken far longer.
One small habit
After editing JSON by hand, run it through a validator before you save it or send it to anyone. The most common hand-editing slip is forgetting the comma before a newly added line, and that single character is enough to make the whole file unreadable.