JSON won’t parse: the eight mistakes that cause it every time

Unexpected token. SyntaxError: JSON.parse. invalid character. JSON parse errors are maddening because the message rarely tells you what is wrong, only that something is wrong at a position in a blob of text. In practice, almost every failure comes from one of eight mistakes.

1. Trailing commas

JavaScript lets you write [1, 2, 3,] with a trailing comma. JSON does not. The last item in an array or object must have no comma after it. This is the single most common cause of JSON failures in hand-edited configs.

2. Single quotes

JSON requires double quotes. 'name': 'Alice' is valid JavaScript but invalid JSON. Every string — keys and values — must use double quotes.

3. Comments

JSON has no comment syntax. // this is the config will break parsing. If you need annotated config files, consider JSONC, YAML, or TOML instead.

4. Unescaped special characters

Newlines, tabs, backslashes and double quotes inside strings must be escaped: , , \, ". A literal newline inside a JSON string is invalid.

5. Invalid number formats

JSON numbers cannot have leading zeros (07 is invalid), cannot be NaN or Infinity, and cannot be hex literals (0xFF). Convert to decimal integers or quote them as strings.

6. Undefined and non-JSON values

undefined, NaN, Infinity, and JavaScript Date objects are not valid JSON values. JSON.stringify silently drops undefined values. If your parsed JSON is missing fields, this is likely why.

7. BOM or invisible characters

Files saved by some Windows editors include a UTF-8 BOM at the start. Many JSON parsers reject it. If your file looks correct visually but fails on “unexpected character at position 0”, strip the BOM.

8. Truncated API output

If the JSON was generated rather than hand-written, check whether it was truncated. Log the raw response body and check its length — a JSON error on API output often means a network or timeout issue upstream, not a syntax problem in your code.

A JSON linter finds all of these in under a second. Paste your JSON into the validator at ateByteApps DevTools and it will show you the exact line where parsing fails.

Leave a Reply

Your email address will not be published. Required fields are marked *