How to Validate and Format JSON (Common Errors Fixed)

Try it yourself: Json Formatter tool →

The five usual suspects

Trailing comma after the last property in an object or array. JSON has no tolerance for it, unlike JavaScript.

Single quotes around keys. JSON requires double quotes for every string, including property names.

Unquoted keys. { name: 'Ada' } is valid JavaScript but invalid JSON - it must be { "name": "Ada" }.

Comments. JSON has no comment syntax, so any // or /* */ block breaks the parse.

NaN or undefined. Both are JavaScript concepts with no JSON equivalent - use null instead.

How to validate automatically

In Node: JSON.parse() throws with the exact position when the input is invalid. Wrap it in try/catch and log the error message.

In Python: json.loads() raises JSONDecodeError with line and column numbers, which points straight at the problem.

In CI: pipe your JSON files through a validator as a build step. Catching a malformed config at build time costs minutes instead of a production incident.

Pretty print vs minify

Pretty-printed JSON with 2-space indent is for humans - config files, API docs, debugging. It costs bytes and is worth it.

Minified JSON is for transport. Removing whitespace typically cuts payload size by 20 to 40 percent with no change to the data.

Sponsored — ad slot reserved

More from TextKit