Developer · How-to
How to Format JSON — A Developer's Guide
Every developer wrestles with JSON: minified blobs from APIs, malformed logs, config files with mysterious errors. This guide covers formatting, validating, comparing, and transforming JSON — plus the specific error messages you'll see and how to fix them.
The 30-second fix
- Open the JSON Formatter.
- Paste your JSON.
- It validates instantly. If invalid, you'll see the line and column of the error.
- Click Prettify for indented output or Minify for compact.
JSON syntax rules (the ones people forget)
- Strings must use double quotes:
"key", not'key'. - Object keys must be double-quoted strings — even if they look like identifiers.
- No trailing commas after the last element of an object or array.
- No comments (
//or/* */). Use JSON5 or JSONC if you need them. - Numbers can't have leading zeros or a trailing decimal point.
- Only these literals:
true,false,null.
Common errors and their fixes
| Error | Cause |
|---|---|
| Unexpected token ' | Single quotes — replace with double quotes. |
| Unexpected token } | Trailing comma before }. |
| Unexpected end of JSON input | Missing closing } or ]. |
| Unexpected token N | Bare NaN or Infinity — not valid JSON. |
| Unexpected token u | Bare undefined — use null. |
Prettify vs minify
Prettified JSON is human-readable with indentation and newlines — use during development and in Git-committed config files. Minified JSON strips all whitespace — use in API responses and network payloads to save bytes (a large document can shrink 20–40%). Both are byte-different but semantically identical.
Beyond formatting
- Compare two JSON documents structurally with a JSON diff — key order doesn't matter, real changes do.
- Convert to CSV to open in Excel, or to YAML for config files.
- Generate TypeScript interfaces from a sample response — Toolzer's JSON → TS tool infers types automatically.
- Validate against a schema with JSON Schema — the standard way to enforce structure in APIs.
Command-line power tools
# jq — the swiss army knife
cat data.json | jq '.' # prettify
cat data.json | jq -c '.' # minify
cat data.json | jq '.users[] | .email' # extract paths
# Node one-liner
node -e "console.log(JSON.stringify(require('./data.json'), null, 2))"Try it on Toolzer
- JSON Formatter — prettify, minify, validate.
- JSON Minifier — strip whitespace for production.
- JSON Diff — compare two JSON documents.
- JSON → CSV — flatten to spreadsheets.
- JSON → TypeScript — generate interfaces.
Frequently asked questions
What is JSON?+
JavaScript Object Notation — a text format for structured data using objects {}, arrays [], strings, numbers, booleans, and null. It's the lingua franca of web APIs.
What does 'invalid JSON' actually mean?+
Usually one of: trailing commas, single quotes instead of double, unquoted keys, missing brackets, or a stray comment. JSON is strict — no comments, no trailing commas, keys must be double-quoted strings.
Should I minify or prettify?+
Prettify during development for readability. Minify for production/network transfer to save bytes. Both are lossless transforms.
How do I compare two JSON files?+
Use a JSON diff tool that ignores key order and whitespace but flags real value/structure differences. Toolzer's JSON Diff does exactly this.
Is JSON5 the same as JSON?+
No. JSON5 allows comments, trailing commas, and unquoted keys — useful for config files but not compatible with strict JSON parsers.
How large a JSON file can browsers handle?+
Modern browsers parse 100+ MB JSON files without issue, though the UI may freeze during parsing. For huge datasets, stream with a JSON parser like Oboe or ijson.
