How to Format, Validate and Minify JSON
JSON (JavaScript Object Notation) is the default format for APIs, configuration files, logs and data exchange. It looks simple, which is exactly why small mistakes are so easy to make and so hard to find. A single misplaced comma can break a 400-line document.
A JSON formatter does three jobs at once: it validates your syntax, it pretty-prints the document so a human can read it, and it can minify it to save space. This guide explains how each of those works, and builds a workflow you can use every time an API response or config file misbehaves.
The three things a formatter does
Validation runs a parser over the document and either returns the parsed value or a precise error location. A good validator tells you the line and character where parsing failed, which is half the battle.
Pretty-printing adds indentation and puts one key-value pair on each line. The data does not change - only whitespace does - so a pretty-printed document carries exactly the same information in a shape your eyes can follow.
Minifying removes all insignificant whitespace, collapsing the document to one line. That is what you usually want to send over a network or store in a build artifact.
{"name":"SwiftTools","tools":50,"free":true}{
"name": "SwiftTools",
"tools": 50,
"free": true
}Common JSON syntax errors
Nearly every "my JSON is broken" case is one of these. They all parse fine in JavaScript, which is how they sneak in:
- Trailing comma - a comma after the last item in an array or object. JSON does not allow it, even though many languages do.
- Unquoted keys - { name: "x" } is invalid; keys must be double-quoted strings.
- Single quotes - JSON only accepts double quotes around string values.
- Comments - neither // nor /* */ is part of the format, and they always fail validation.
- NaN, Infinity and undefined - none of these exist in JSON; use null or omit the field.
- Duplicate keys - technically parseable, but most tools keep only the last value, which silently hides data.
A workflow for debugging API responses
When something "just does not work", keep this sequence in mind:
- 1Capture the raw response - from your client code, a REST client, or the browser network tab. Save it exactly as received; do not edit it yet.
- 2Validate it. If it parses, your problem is not the syntax but the shape or values - jump straight to reading the formatted output.
- 3Pretty-print it. With indentation, structure becomes visible: you can see at a glance which nesting level is not what you expected.
- 4Search the formatted document for the field your code expects. Mismatched key names (snake_case vs camelCase) are a classic cause of "null" values.
- 5Minify it if you need to paste it into a bug report - one line is far easier to share than 300.
Formatting versus parsing
A document can be valid JSON and still not do what you expect. Formatting only makes structure visible; it cannot tell you that a field has the wrong type or that a value is off by a factor of ten. When the document validates but behaves wrongly, switch from format tools to data tools: check the types of the fields you rely on, and compare the payload against the documented schema.
The adjacent problems in most developer workflows are encoding and identity. URL encoding handles query parameters that contain JSON; Base64 appears in auth headers and data URIs; UUIDs are usually how the objects you are formatting are named in the first place. The URL Encoder, Base64 tool and UUID generator cover all three without leaving the browser.
Frequently asked questions
Is it safe to paste sensitive JSON into an online formatter?
It depends on where the document is processed. Formatters that run entirely in your browser never send your data to a server at all. For tokens, credentials or customer data, prefer in-browser or local tools over server-side ones.
Why does my JSON fail in the formatter but worked in the API?
Usually copy-paste damage: smart quotes, non-breaking spaces or HTML entities introduced by the page you copied from. Re-paste from the raw source, or save the raw response to a file first.
What is the difference between JSON and JSON5?
JSON5 is a relaxed superset that allows comments, single quotes, unquoted keys and trailing commas. It is convenient in code, but many servers and tools only accept strict JSON - if your JSON5 document fails validation, that is usually why.
Does minifying change the data?
No. Minifying removes only insignificant whitespace; every key, value and nesting level is preserved.
Want to try it?
The JSON Formatter is free and runs right in your browser (developer tools). Open it now and see the guide in action.
Try the JSON Formatter