JSON formatting explained: valid vs. pretty vs. minified
JSON is the lingua franca of APIs and config files. It's simple by design, but a single stray comma can break an entire payload. Here's a practical guide to reading, writing, and fixing it.
What makes JSON valid
Valid JSON follows a small, strict set of rules:
- Data is written as key/value pairs inside objects
{ }, or as ordered lists inside arrays[ ]. - Keys must be double-quoted strings. Single quotes are not allowed.
- Values can be a string, number, boolean (
true/false),null, object, or array. - No trailing commas after the last item — the single most common error.
- No comments. Standard JSON doesn't support them.
Pretty vs. minified — same data, different shape
Pretty-printed JSON adds indentation and line breaks so humans can read it. Minified JSON strips all that whitespace to make the payload as small as possible for transmission. They're semantically identical — a formatter just toggles between them. Use pretty for debugging and documentation; use minified for production API responses where every byte counts.
The errors that bite most often
- Trailing comma:
{"a":1,}— remove the last comma. - Single quotes:
{'a':1}— JSON requires double quotes. - Unquoted keys:
{a:1}— keys must be quoted. - Unescaped characters: a literal newline or quote inside a string must be escaped (
\n,\"). - Missing bracket: an unclosed
{or[— a formatter will point you to where the structure breaks.
How a formatter helps
A good JSON formatter does three jobs at once: it validates (tells you exactly where the syntax is wrong), beautifies (indents it so you can see the structure), and minifies (collapses it for shipping). Pasting a broken payload in and reading the error is usually the fastest way to find that one misplaced comma.
A tip for big payloads
When an API returns a huge blob on one line, formatting it first makes everything else — spotting the field you need, checking nesting, diffing two responses — far easier. For comparing two versions, format both, then run them through a text diff.