JSON Formatting and Validation: The Rules That Actually Matter

Most "broken JSON" comes down to a small handful of syntax rules that are easy to violate without noticing.

Keys must be double-quoted strings

JSON requires double quotes, not single quotes, for both keys and string values. Unlike JavaScript object literals, unquoted keys and single-quoted strings are invalid JSON, not just an unconventional style.

No trailing commas allowed

A comma after the last item in an object or array is one of the most common reasons a JSON file fails to parse, especially when copied from JS code where trailing commas are often fine.

Only a few value types exist

JSON values must be strings, numbers, booleans (true/false), null, objects, or arrays. undefined, functions, comments, and dates-as-objects are not valid JSON and will fail validation.

Pretty-printed and minified are the same data

Formatting with indentation makes JSON readable for humans, while minifying strips unnecessary whitespace to shrink file size for transmission. Both represent identical data, just formatted differently.

Objects and arrays can nest freely

A JSON value can itself contain further objects or arrays to any depth, which is how JSON represents complex, hierarchical data like API responses.

Why JSON is strict on purpose

JSON was designed as a simple, unambiguous data-interchange format, so its strictness is a feature rather than a limitation β€” it guarantees that any conforming parser, in any language, interprets the same file identically.

Where JSON errors usually come from

Hand-edited config files, text copy-pasted from JavaScript or Python (where quoting rules differ), trailing commas left over from editing, and // comments (which plain JSON does not support at all) account for the large majority of validation failures.

Frequently Asked Questions

Can JSON have comments?

No, standard JSON has no comment syntax. Some tools support JSON5 or JSONC extensions, but a plain .json file with // or /* */ comments will fail a strict validator.

Why does my JSON work in JavaScript but fail validation?

JavaScript object literals are more permissive β€” unquoted keys, single quotes, trailing commas, and comments are all valid JS syntax, but none of them are valid JSON, so code that runs fine in a .js file can still fail a strict JSON parser.