JSON Formatting Best Practices Every Developer Should Know
JSON looks simple enough that it's easy to underestimate how often it breaks things. A missing comma, an unescaped quote, or a trailing comma left over from editing can silently fail an API call or crash a build step. Getting comfortable with how JSON is structured — and how to debug it quickly — saves a surprising amount of time.
The rules that actually matter
- Property names must be wrapped in double quotes, never single quotes.
- Trailing commas after the last item in an object or array are not allowed.
- Comments are not part of the JSON spec — no // or /* */ blocks.
- Numbers can't have leading zeros, and there's no way to represent NaN, Infinity, or undefined.
- Strings must use double quotes; backslashes and quotes inside a string need to be escaped.
Most JSON errors come from violating one of these rules after copying data from somewhere else — a JavaScript object literal that allowed single quotes and trailing commas, for instance, will look almost identical to valid JSON but fail to parse.
Reading a validation error
When a JSON parser fails, it usually reports a position — a line and column, or a character offset — where it got confused. That position is not always exactly where the actual mistake is. A missing closing brace, for example, often shows up as an error several lines later, at the point where the parser ran out of valid structure to make sense of.
A reliable way to narrow down the problem is to comment out or delete the second half of the document and see if the first half parses. Bisecting like this — repeatedly splitting the suspect region in half — finds the exact broken character far faster than scanning visually, especially in large, deeply nested payloads.
Formatting for readability
Minified JSON — all on one line, no whitespace — is efficient to transmit over a network but painful to read. Pretty-printing with consistent indentation (commonly two or four spaces) turns a wall of characters into a structure you can actually scan.
Most languages provide this for free. In JavaScript, JSON.stringify accepts a third argument that controls indentation — JSON.stringify(data, null, 2) produces nicely formatted output with two-space indents. Python's json module offers the equivalent indent parameter on json.dumps.
Common gotchas with nested data
Deeply nested JSON — objects inside arrays inside objects — is where formatting matters most, because a single misplaced bracket can be invisible in minified form but obvious once indented. If you're debugging a deeply nested structure, format it first before trying to spot the error visually.
Another frequent issue is duplicate keys. The JSON spec doesn't forbid them, but most parsers will silently keep only the last occurrence, discarding earlier ones without any warning. If a field seems to be returning an unexpected value, check whether the key appears more than once in the source.
A quick workflow
When debugging a JSON issue: paste the raw payload into a formatter to get clean indentation, read the specific error message and position if validation fails, bisect the document if the error location seems wrong, and double check for the classic offenders — trailing commas, single quotes, and unescaped characters inside strings.