How to Format and Validate JSON:
Quick Tutorial
Turn a one-line JSON blob into readable, indented output in three steps — and learn to read parse errors so broken JSON takes seconds to fix, not minutes.
This tutorial walks you through formatting a messy JSON blob into readable, indented output — and validating it at the same time. You'll paste raw JSON, format it, read the error message if it's broken, and fix the three mistakes that cause 90% of JSON parse failures. Total time: two to three minutes.
Step 1: Paste Your JSON
Open the JSON Formatter and paste your JSON into the input panel. It doesn't matter how mangled it looks — single line from an API response, log excerpt with escaped quotes, or output copied from a terminal. Everything runs locally in your browser, so pasting API responses that contain internal data is safe: nothing is uploaded.
A typical starting point looks like this — one line, impossible to scan:
{"user":{"id":42,"name":"Ada","roles":["admin","dev"],"active":true},"meta":{"page":1,"total":137}}
Step 2: Format It
Click Format. If the JSON is valid, you get indented output:
{
"user": {
"id": 42,
"name": "Ada",
"roles": ["admin", "dev"],
"active": true
},
"meta": {
"page": 1,
"total": 137
}
}
Formatting is also validation. The formatter parses your input with a real JSON parser first — if parsing succeeds, the JSON is valid by definition. There is no such thing as JSON that formats correctly but is invalid.
Most formatters let you pick the indent width. Two spaces is the JavaScript ecosystem default (what JSON.stringify(obj, null, 2) produces); four spaces is common in Python projects. Pick whichever matches the codebase you're pasting into.
Step 3: Read the Error If It Fails
If the input is invalid, you get a parse error with a position, something like Unexpected token ' in JSON at position 12. The position is a character offset from the start of the input. Count to that offset (or paste into an editor that shows column numbers) and look at what's immediately before it — the actual mistake is usually the character right before the reported position.
Three mistakes cause the vast majority of failures:
- Single quotes.
{'name': 'Ada'}is valid JavaScript but invalid JSON. JSON requires double quotes on both keys and string values. - Trailing commas.
{"a": 1, "b": 2,}fails. The comma after the last item is legal in modern JavaScript and in JSON5, but not in JSON itself. - Unquoted keys.
{name: "Ada"}fails. Every key must be a double-quoted string.
Two more that show up in real-world debugging: NaN, Infinity, and undefined are not valid JSON values (a Python or JS serializer misconfiguration produces these), and comments (// or /* */) are not allowed — if your "JSON" file has comments, it's actually JSONC, which VS Code config files use but JSON.parse rejects.
Worked Example: Debugging an API Response
You call an API with curl and get back a wall of text. Here's the workflow:
curl -s https://api.example.com/v1/orders/9001
- Copy the response body and paste it into the formatter.
- Click Format. Suppose you get:
Unexpected token < in JSON at position 0. - Position 0 and a
<means the response starts with an HTML tag — the server returned an HTML error page (usually a 404 or a proxy error) instead of JSON. The bug isn't your JSON handling; check the URL and the response status code with the HTTP Status reference. - Fix the URL, re-run curl, paste again. This time it formats cleanly, and the indented structure shows you that
order.itemsis an empty array — which is the actual bug you were hunting.
This "format first, read second" habit is the fastest way to debug any JSON-producing system: the formatter tells you immediately whether the problem is malformed syntax or unexpected-but-valid data.
Formatting JSON Without the Tool
When you're in a terminal, pipe through one of these:
# Python — preinstalled on macOS and most Linux
curl -s https://api.example.com/data | python3 -m json.tool
# jq — the standard JSON CLI (brew install jq)
curl -s https://api.example.com/data | jq .
# Node.js one-liner
cat data.json | node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(0)), null, 2))"
jq is worth installing if you handle JSON daily: jq . formats, jq '.user.roles' extracts a field, and jq 'keys' lists object keys. The browser tool wins when you want to visually explore nested structure, need to share formatted output with someone, or don't have terminal access to where the JSON lives.
Common Problems and Fixes
The JSON is valid but has escaped quotes everywhere
Output like "{\"name\": \"Ada\"}" means you have a JSON string containing JSON — double-encoded. This happens when code calls a serializer on an already-serialized string. Unescape it once with the JSON Escape tool, then format the result. If your logs show this, the fix in code is to remove the extra serialize call.
Formatting works but numbers changed
If "id": 9007199254740993 comes out as 9007199254740992, you've hit JavaScript's number precision limit (253). Any browser-based formatter parses numbers as 64-bit floats, so integers above 9,007,199,254,740,991 lose precision. The upstream fix is to send large IDs as strings.
File is too big and the browser stalls
Browser formatters handle a few megabytes fine but struggle past 10–50 MB. For big files, use jq . big.json > formatted.json in a terminal instead — it streams and won't freeze.
I need to compare two JSON payloads, not just format one
Format both first so the diff isn't drowned in whitespace changes, then use the JSON Compare tool, which diffs by structure rather than by line.
FAQ
Does formatting change my data?
No. Formatting only changes whitespace between tokens. Key order, values, and nesting are untouched. One caveat: as noted above, numbers beyond 253 lose precision in any JavaScript-based tool — that's a parsing effect, not a formatting one.
What's the difference between formatting and minifying?
Opposites. Formatting adds whitespace for humans; minifying strips every optional space and newline for machines, shrinking payload size typically 20–40%. The same parser sits under both operations.
Why does my JSON work in JavaScript but fail validation?
Because JavaScript object literal syntax is a superset of JSON. Single quotes, unquoted keys, trailing commas, and comments all work in JS source code but violate the JSON spec (RFC 8259). If you need those conveniences in config files, look at JSON5 or YAML — and see the JSON structure guide for the full syntax rules.
Is it safe to paste production API responses into an online formatter?
Into this one, yes: it parses and formats entirely client-side with no network calls. As a general habit, check any online tool's privacy claims before pasting data containing tokens or personal information — or use the terminal commands above, which never leave your machine.