SyntaxError: Unexpected token in JSON — what it means and how to fix it

The parser reached a character that cannot appear at that point in JSON, so it stopped. The “token” is that first bad character, and “position N” is a zero-based character offset from the start of the text (Chrome now also prints the line and column). PasteKit converts the offset to a line and column and highlights it, which is usually enough to see what is wrong.

Seen as:

  • SyntaxError: Unexpected token 'a', ...""status": active, "... is not valid JSON
  • SyntaxError: Unexpected token a in JSON at position 28
  • SyntaxError: JSON.parse: unexpected character at line 3 column 13 of the JSON data
  • SyntaxError: JSON Parse error: Unexpected identifier "active"
  • json.decoder.JSONDecodeError: Expecting value: line 3 column 13 (char 28)

Input

Settings

History

Load from URL

Common causes

1. A string value without double quotes

Every string in JSON must be wrapped in double quotes. A bare word such as active, yes or pending is read as an unknown token; only true, false and null may appear unquoted.

Before
{"id": 1042, "status": active}
After
{"id": 1042, "status": "active"}

2. Single quotes or a JavaScript object literal

Text copied from JavaScript or Python source often uses single quotes or unquoted keys. JSON accepts neither; see single quotes in JSON for the full story.

Before
{'id': 1042, status: 'active'}
After
{"id": 1042, "status": "active"}

3. Number spellings JSON does not allow

JSON numbers cannot start with + or ., cannot have leading zeros and cannot be hexadecimal. Write plain decimal digits, or quote the value if it is really an identifier.

Before
{"offset": +5, "ratio": .75, "mask": 0xFF, "zip": 02139}
After
{"offset": 5, "ratio": 0.75, "mask": 255, "zip": "02139"}

4. The text is not JSON at all (position 0)

When the bad token is at position 0, the whole body is something else: plain text such as Internal Server Error, an HTML page (see Unexpected token ‘<’) or an empty string. Log the raw response before parsing it.

Before
Internal Server Error
After
{"error": "Internal Server Error", "status": 500}

Frequently asked questions

What does "position 0" mean in this error?

Positions count characters from the start of the text, starting at 0. Position 0 means the very first character was already wrong, so the input is probably not JSON at all, for example an HTML error page, plain text or the word undefined.

How do I find position 1234 in a large file?

Paste the text into PasteKit: it reports the line and column instead of a raw offset and highlights the spot. Newer Chrome and Node versions also print “(line X column Y)” after the position.

Why does the same text work in my JavaScript code?

JavaScript object literals are more permissive than JSON: they allow single quotes, unquoted keys, trailing commas and comments. JSON.parse only accepts the strict JSON grammar.

Can PasteKit repair the input automatically?

Yes. Fix it handles unquoted strings and keys, single quotes, trailing commas, comments and Python literals, and shows a diff of every change before you accept it.

Related