How to Format and Validate JSON: A Practical Guide
JSON (JavaScript Object Notation) is the lingua franca of modern APIs — nearly every web service sends and receives it. But JSON has strict syntax rules, and a single misplaced comma or missing quote can make the whole document invalid. Knowing how to format and validate JSON quickly is a core developer skill.
What JSON formatting actually does
A JSON formatter takes a compact or inconsistently indented JSON string and rewrites it with consistent indentation and line breaks. The result is semantically identical — the same data, the same structure — just easier to read. Most formatters default to 2-space or 4-space indentation.
This matters in practice because APIs often return minified JSON to save bandwidth, and log aggregation tools often compact JSON before storing it. When you need to inspect a response or debug a payload, you almost always want the formatted version first.
What JSON validation checks
Validation means checking that the document conforms to the JSON specification (ECMA-404 / RFC 8259). A validator will catch:
- Trailing commas after the last element in an object or array
- Missing commas between elements
- Unquoted or single-quoted property keys
- Unclosed brackets or braces
- Control characters in strings that aren't escaped
- Numbers written in non-standard notation (e.g.
0x1Fhex literals) - Comments — JSON does not support
//or/* */comments
Common JSON errors and how to fix them
Trailing commas
This is the most common mistake, especially for developers coming from JavaScript where trailing commas are legal:
{ "name": "Ada", "role": "Engineer", }
The fix: remove the comma after the last property. JSON does not allow trailing commas in objects or arrays, even though JavaScript and TypeScript do.
Single-quoted strings
JSON requires double quotes for both keys and string values. Single quotes are not valid:
{ 'name': 'Ada' } — invalid
{ "name": "Ada" } — valid
Comments
JSON was deliberately designed without comments. If you need annotated config files, consider JSONC (JSON with Comments, supported by VS Code and some tools), YAML, or TOML instead. A plain JSON parser will reject any document containing // or /* */.
Mismatched brackets
Large nested documents are easy to get wrong. A good JSON formatter will highlight exactly which line has the mismatched bracket, saving you from scrolling through hundreds of lines manually.
Formatting vs. minifying
Minifying is the opposite of formatting: it strips all whitespace to produce the most compact possible JSON. This is useful when you need to embed a JSON payload in an environment variable, pass it as a URL query parameter, or reduce the size of a file you're shipping. The data is the same; only the whitespace changes.
JSON Schema validation
Beyond syntax checking, JSON Schema lets you validate the structure of a document — that the right fields are present, that values are the right types, that numbers are within range. This is a separate concern from formatting; a document can be syntactically valid JSON but still be missing required fields your application depends on. Most backend frameworks have JSON Schema validators built in or available as libraries.
When to use a formatter tool
A browser-based formatter is useful in a few specific situations: inspecting an API response you copied from DevTools, debugging a payload that a service returned as a single line, checking whether a config file you hand-edited is still valid, or quickly reading a log entry that got compacted. For anything in a CI pipeline or automated test, use a language-native JSON parser instead — it will give you structured errors tied to your code.