How to Format and Validate JSON: A Developer Guide
JSON is the default format for APIs, config files, and data interchange, but a single misplaced character can break an entire payload. Whether you are debugging a failed API response or cleaning up a config file, knowing what valid JSON requires and how to read a parse error will save you a lot of guesswork. This guide walks through the rules, the formatting choices, and the common mistakes that trip people up.
What Valid JSON Actually Requires
JSON has a small, strict grammar. Getting it right comes down to a handful of rules that the parser enforces without exception:
- Keys must be strings in double quotes. Single quotes are not allowed, and unquoted keys, which are legal in JavaScript object literals, are rejected.
- String values also use double quotes. If a string contains a double quote or a backslash, escape it with a backslash.
- No trailing commas. The last item in an array or the last pair in an object must not be followed by a comma.
- No comments. Unlike YAML or JavaScript, the JSON spec has no comment syntax, so any inline notes will cause a failure.
- Only specific value types are permitted: strings, numbers, the literals true, false, and null, plus arrays and objects. Values like undefined, NaN, or infinity are invalid.
Numbers follow their own rules too. They cannot have leading zeros, and a decimal point must be followed by at least one digit. These constraints are why hand-written JSON so often fails on the first try.
Pretty-Printing vs. Minifying
The same JSON data can be displayed two ways, and each serves a different purpose. Pretty-printing adds indentation and line breaks so the structure is easy to scan. Minifying strips every optional space and newline to make the payload as small as possible.
When to Pretty-Print
Use pretty-printing while you are reading, debugging, or editing. Nested objects and long arrays become far easier to follow when each level is indented, and version-control diffs stay readable because each value sits on its own line. Config files that humans maintain should almost always be formatted this way.
When to Minify
Minify when the JSON travels over the network or gets stored at scale. Removing whitespace shrinks response sizes, which speeds up API calls and reduces bandwidth. Production API responses and embedded data are common places to minify, since no human needs to read them directly.
You do not have to choose one forever. A good tool lets you toggle between the two, so you can minify for delivery and pretty-print the same data when you need to inspect it.
Reading a Validation Error by Line and Column
When a parser rejects JSON, it usually reports a position, something like an unexpected token at a given line and column. That location is your starting point, but it is not always the exact source of the mistake. Parsers report the error where they first notice something is wrong, which is often just after the real problem.
A classic example is a missing comma between two array items. The parser reads the first value fine, then reaches the second value and complains, even though the fix belongs on the previous line. When you land on the reported line and column, check the character right before it as well. Look for an unclosed bracket, a missing comma, or a stray quote just upstream of the flagged position.
Common Causes of Parse Errors
Most failures fall into a few repeat offenders. Scan for these before anything else:
- Trailing commas left behind after deleting the last element of an array or object.
- Single quotes copied from JavaScript or Python source instead of double quotes.
- Unescaped characters inside strings, especially double quotes and backslashes in Windows file paths.
- Comments pasted in from a config file or documentation that were never valid JSON.
- Mismatched brackets or braces, where an opening symbol never gets its closing partner.
- Truncated payloads, where a copy-paste or a failed download cut the data off mid-structure.
Let a Formatter Do the Work
Hunting for a single misplaced character by eye is slow and error-prone. A dedicated tool parses the input, points you straight to the failing line and column, and reformats valid data at the click of a button. Paste your data into the JSON Formatter to validate it, pretty-print it for reading, or minify it for shipping. It turns a frustrating hunt into a quick fix, and it never sends your data anywhere, since the parsing happens right in your browser.