JSON Formatter Guide: Pretty-Print, Minify & Validate
JSON Formatter Guide: Pretty-Print, Minify & Validate
JSON (JavaScript Object Notation) is the de facto format for exchanging data between APIs, configuration files, and front-end applications. But raw JSON from a network response is often a single dense line that is painful to read. A JSON formatter turns that wall of text into something a human can actually scan — and validates it at the same time.
This guide explains how formatters work, the difference between pretty-print and minify, indentation conventions, JSON5/JSONC, the most common validation errors, and how to sort keys. Every example runs in your browser with our JSON Formatter — no data ever leaves your machine.
What Is a JSON Formatter?
A JSON formatter is a tool that parses a JSON string into an in-memory data structure and then prints it back out in a chosen layout. Because it has to parse the input first, it inherently validates the syntax: if the input is not valid JSON, the formatter tells you exactly where it broke instead of silently producing garbage.
The core operations are:
- Pretty-print — add indentation and line breaks so nested objects are easy to read.
- Minify — remove all whitespace to make the payload as small as possible.
- Validate — confirm the string conforms to the JSON grammar.
- Sort keys — reorder object keys alphabetically for stable, diff-friendly output.
Pretty-Print vs Minify
These are opposites, and choosing the right one matters.
| Mode | Whitespace | Use case |
|---|---|---|
| Pretty | Newlines + indent | Debugging, config files, code review |
| Minify | None | API payloads, storage, embedding in URLs/attributes |
Pretty-printing a 2 KB response may turn it into 40 lines, but it makes a nested
user.address.city path obvious at a glance. Minifying the same object collapses
it to one line and can shave 30–50% off the byte size when whitespace is stripped.
Rule of thumb: pretty-print while developing, minify while shipping.
Indentation Rules
The JSON specification does not mandate indentation, but two conventions dominate:
- 2 spaces — the default in most web tooling (Prettier, Node, JS ecosystem).
- 4 spaces or tabs — common in Java / Python tooling and editor defaults.
Our formatter lets you pick the indent size and whether to use spaces or tabs. Stick to one convention per project; mixed indentation makes diffs noisy.
{
"name": "CodeToolPro",
"tools": 175,
"categories": ["converters", "formatters", "testers"]
}
JSON5 and JSONC
Plain JSON is strict: no comments, no trailing commas, no unquoted keys. Two relaxed supersets show up constantly:
- JSON5 — allows comments, trailing commas, unquoted keys, and single quotes. Popular in build configs.
- JSONC — "JSON with Comments", used by
tsconfig.jsonand VS Code settings.
If your input has // comments or a trailing comma, a strict JSON formatter will
reject it. Strip the comments first, or use a JSON5-aware parser.
Common Validation Errors
These are the failures a formatter catches most often:
- Trailing comma —
"a": 1, }is invalid in strict JSON. - Single quotes — JSON requires double quotes around keys and string values.
- Unquoted keys —
{ name: "x" }must be{ "name": "x" }. - Comments —
//and/* */are not allowed in strict JSON. - Wrong literal case — it is
true/false/null, neverTrue/None. - Duplicate keys — technically parseable, but ambiguous; many parsers keep the last.
When validation fails, read the error position. "Unexpected token at line 12" almost always means a missing comma, an unclosed brace, or a stray character just before that line.
Sorting Keys
Sorted keys make JSON deterministic. This matters for:
- Cache keys — two objects with the same data but different key order produce different hashes.
- Code review — alphabetical order makes it easy to spot what actually changed.
- Config drift detection — sorted output diffs cleanly between environments.
{ "b": 2, "a": 1 } → { "a": 1, "b": 2 }
Code Examples
JavaScript
// Pretty-print
JSON.stringify(data, null, 2);
// Minify
JSON.stringify(data);
// Detect invalid JSON
try {
JSON.parse(input);
} catch (e) {
console.error("Invalid JSON:", e.message);
}
Python
import json
# Pretty-print
print(json.dumps(data, indent=2, sort_keys=True))
# Minify
print(json.dumps(data, separators=(",", ":")))
# Validate
try:
json.loads(input)
except json.JSONDecodeError as e:
print("Invalid JSON:", e)
Both runtimes use the same grammar, so a string valid in one is valid in the other.
Related Tools
- Format a single object with the JSON Formatter.
- Convert JSON to TypeScript interfaces with JSON to TS.
- Validate structure against a contract with JSON Schema.
- Pretty-print YAML or TOML config with the YAML Formatter and TOML Formatter.
When to Use a Formatter Instead of Code
You can pipe through jq or write a script, but a browser formatter wins when
you just received a blob of JSON in a chat, a log, or a response body and want to
read it now — no install, no clipboard of secrets sent to a server. For
repeatable pipelines, keep the script; for ad-hoc inspection, use the tool.