How to Fix Invalid JSON: Every Common JSON.parse() Error, Explained
JSON.parse() fails loudly but not always clearly. The error message tells you where it broke, not why in plain English. Here's a translation for the ones you'll actually hit, and the fastest way to find the exact spot: paste the payload into the JSON Formatter, which reports the precise line and column instead of just a character offset.
"Unexpected token ) in JSON at position X"
Almost always a trailing comma: {"a": 1, "b": 2,}. Valid in a JavaScript object literal, invalid in JSON. Remove the comma before the closing brace or bracket.
"Unexpected token ' in JSON at position X"
JSON requires double quotes, not single quotes, around strings and keys. {'a': 1} needs to become {"a": 1}. This one usually shows up when JSON gets hand-copied out of JavaScript source code, where single quotes are valid.
"Unexpected token a in JSON at position X" (or any bare letter)
Usually an unquoted key: {a: 1} is JavaScript object shorthand, not JSON — keys need quotes too: {"a": 1}.
"Unexpected end of JSON input"
The input is truncated: a string that got cut off mid-response, an empty string being parsed, or a closing brace/bracket that's missing entirely. Check the very end of the payload; a network response cut short by a timeout is a common real-world cause.
"Unexpected token u in JSON at position 0"
This is JSON.parse(undefined) in disguise: undefined gets coerced to the string "undefined", and JSON parsing chokes on the letter u. Check whatever produced the input; you're parsing a value that was never actually set.
Unescaped control characters inside a string
A literal newline or tab character inside a JSON string value breaks parsing; it needs to be the escape sequence \n or \t instead. This tends to happen when JSON is built by hand-concatenating strings rather than using a proper serializer.
The fastest way to actually find it
- Paste the full payload into the JSON Formatter.
- Read the reported line and column, not just the error type.
- Fix that one spot; malformed JSON usually has exactly one break point, and everything after it just looks broken as a side effect.
If the payload is valid JSON but you're not sure it should look the way it does, generating a JSON Schema from a known-good example gives you something to validate future payloads against, instead of eyeballing it each time.