JSON Formatting: A Practical Guide
Learn how to format, validate, and debug JSON — from fixing 'unexpected token' errors to choosing between minified and pretty-printed output, with copy-paste examples.
Updated July 2026 · 6 min read
JSON (JavaScript Object Notation) is the default language of APIs, config files, and data exchange on the web. It is easy to produce but surprisingly easy to break: one stray comma or a single quote in the wrong place turns a valid payload into a parser error. Formatting JSON — re-indenting it into a clean, readable shape — is the fastest way to spot those mistakes and understand the structure of data you did not write yourself.
This guide covers what formatting does, the errors it helps you catch, a reliable debugging workflow, and when to prefer minified over pretty-printed output.
What "formatting" means
Formatting only changes whitespace. Take this minified response from an API:
{"id":42,"name":"Ada","roles":["admin","editor"],"active":true}Formatted with two-space indentation, the same data becomes:
{
"id": 42,
"name": "Ada",
"roles": ["admin", "editor"],
"active": true
}The bytes that matter — keys, values, structure — are identical. Only the line breaks and indentation changed. That is why formatting is safe: it never alters your data, so you can pretty-print a payload, inspect it, and copy it back with confidence.
When you actually need it
- Reading an API response. Logs and network tabs often show minified JSON on a single line. Formatting makes nesting visible so you can find the field you care about.
- Debugging a rejected request. When a server returns
400 Bad Request, the body you sent is usually the culprit. Formatting reveals the structural mistake immediately. - Reviewing config or fixtures. Pretty-printed JSON produces clean line-by-line diffs in pull requests; minified JSON produces one unreadable diff line.
- Sharing data with a teammate. A formatted snippet (or a shareable link) is far easier to reason about together than a wall of text.
The errors formatting helps you catch
A good formatter also validates. When the input is invalid it stops and points to the first problem. These are the mistakes it surfaces most often:
Trailing commas
Valid in JavaScript, illegal in JSON. The comma after "editor" below breaks parsing:
{
"roles": ["admin", "editor",]
}Single quotes and unquoted keys
JSON requires double quotes around both keys and string values. This is invalid:
{ name: 'Ada' }The correct form is:
{ "name": "Ada" }Comments
JSON has no comment syntax. Lines starting with // or wrapped in /* */ will fail. If you need comments, use a superset like JSON5 or YAML instead, then convert.
Numbers stored as strings
"42" is a string; 42 is a number. This is valid JSON but a frequent source of bugs when your application expects an integer and receives a quoted value. Formatting makes the quotes obvious so you can catch the type mismatch.
A reliable debugging workflow
- Paste the raw JSON into a formatter/validator.
- If it reports an error, jump to the line and column it names. The problem is almost always at or just before that position — a missing quote, an extra comma, or an unclosed bracket.
- Fix one error and re-run. Errors often cascade, so resolve them top to bottom.
- Once it parses, read the formatted output to confirm the structure matches what you expect.
- Copy the clean output back into your code, ticket, or config file.
Minified vs. formatted: which to ship
Both are valid — they differ only in whitespace — so choose based on the audience:
| Use minified when… | Use formatted when… |
|---|---|
| Sending data over the network (smaller payloads) | A human edits or reviews the file |
| Serving API responses at scale | Storing config, fixtures, or translations in git |
| Embedding JSON in a data attribute or URL | Documenting an example in a README or ticket |
Build tools handle this automatically: you keep formatted JSON in source control, and your bundler or server minifies it in production. You rarely need to minify by hand.
Best practices
- Validate before you trust. Never assume a payload is well-formed — parse it first.
- Keep indentation consistent. Two spaces is the most common convention; pick one and let your tooling enforce it.
- Prefer client-side tools for sensitive data. If a payload contains anything private, use a formatter that processes data in the browser rather than uploading it.
- Watch your types. Numbers, booleans, and
nullshould not be quoted unless they are genuinely strings.
Try it
The DevBox JSON Formatter formats and validates entirely in your browser, highlights the first syntax error, and gives you a shareable link so a teammate can open the exact same structure. If you also work with config files, the JSON ⇄ YAML Converter turns a validated payload into clean YAML and back.
Frequently asked questions
What does formatting JSON actually do?+
Formatting (also called pretty-printing or beautifying) re-indents JSON so nested objects and arrays sit on their own lines. It does not change the data — it only changes the whitespace so the structure is readable.
Is it safe to paste sensitive JSON into an online formatter?+
It depends on the tool. DevBox's JSON Formatter runs entirely in your browser and never uploads your input to a server, so it is safe for local debugging. As a general rule, avoid pasting production secrets or personal data into any online tool unless it clearly processes data client-side.
Why does my JSON say 'Unexpected token' when it looks fine?+
The most common causes are trailing commas, single quotes instead of double quotes, unquoted keys, or a comment (// or /* */). JSON allows none of these. A formatter/validator points you to the exact position of the first error.
What is the difference between JSON and a JavaScript object?+
JSON is a text format with strict rules: keys must be double-quoted strings, and only strings, numbers, booleans, null, arrays, and objects are allowed. A JavaScript object literal is code and can contain functions, comments, trailing commas, and unquoted keys — none of which are valid JSON.
Should I commit minified or formatted JSON to source control?+
Commit formatted JSON for anything a human edits (config files, fixtures, translations) so diffs are readable. Use minified JSON for data that is only sent over the wire or served to clients, where size matters.