elefcode
← All guides

Web & APIs · 4 min read

Common JSON Syntax Errors and How to Fix Them

JSON looks like JavaScript, but its rules are much stricter. Most "Unexpected token" and "invalid JSON" errors come from a handful of habits carried over from JavaScript or hand-editing.

Try it yourself with the related tool.

Format and validate JSON →

Advertisement

The rules JSON enforces

Keys must be in double quotes. Strings must use double quotes, never single quotes. There are no trailing commas after the last item, no comments, and no undefined, NaN, or Infinity. Numbers cannot have leading zeros or a leading plus sign. The only literals are true, false, and null, all lowercase.

The usual suspects

A trailing comma — {"a": 1,} — is the most common error, often left after deleting the last line. Next come single quotes from Python output, unquoted keys copied from JavaScript objects, and // comments in config files. Pasted text can also bring curly “smart quotes” that look right but are different characters.

Reading the error message

Parsers report a position: a line and column, or a character offset. The real mistake is usually just before that point — a missing comma shows up as an error on the following key. "Unexpected end of JSON input" means something was cut off: a missing closing bracket, or an empty response from an API that returned an error page instead.

If the first character is <, you received HTML — typically a 404 or login page — not JSON at all.

JSON5 and JSONC

Some config files, such as VS Code settings and tsconfig.json, accept comments and trailing commas. That is JSONC, a relaxed variant. It is fine where the tool supports it, but strip the extras before sending data to an API or a strict parser.

Quick reference

Related guides