Skip to content

JSON "unexpected token" errors: find the line and fix it

The parser stopped at a character JSON does not allow. Here is what each token means, how to find the exact line, and how to fix it.

By Shahzad Ali ·

What the error means

"Unexpected token } in JSON at position 412", "Unexpected end of JSON input", "Unexpected token < in JSON at position 0": every one of these comes from a JSON parser that read something the format does not allow. JSON is strict. One stray comma, a comment, or a single quote is enough, and the parser stops at the first character it cannot accept, which is often one character after the real mistake.

The five usual causes

Read the token in the message; it tells you which one you have:

  • Unexpected token < at position 0: the text is an HTML page, not JSON. A server answered with an error or login page. Open the address in the browser and look at what it really returns.
  • Unexpected token u at position 0: the text is the word undefined. A variable was empty before it was parsed.
  • Unexpected end of JSON input: the text stops early, usually an empty response or a file cut off while saving.
  • Unexpected token } or ]: a trailing comma before a closing bracket ({"a": 1,}), which JavaScript allows and JSON does not.
  • Unexpected token ' or a letter: single quotes, unquoted keys ({name: "x"}), a comment (// or /* */), or the words True, None, NaN or Infinity, which come from Python and are not JSON.

Find the exact spot

The position in the message counts characters from the start, which is useless in a long file. The JSON validator shows the same parser message with the line and column, and Go to error selects that place in the text. Paste the text or drop the .json file; a file with a syntax error opens too, and the error shows right away. Nothing is uploaded: the check runs in your browser.

Fix it automatically when it is one of the usual mistakes

Press Repair. It removes comments, adds missing commas and drops trailing ones, turns single and curly quotes into double quotes, quotes bare keys, and replaces True, False, None, NaN, undefined and Infinity with their JSON equivalents, then says what it changed. Validate again to confirm. Repair does not guess at missing brackets or cut-off files; for those the line and column show where to look.

Once the text is valid, the JSON formatter indents it or minifies it to one line, and the tree view shows the structure.

A byte order mark at position 0

A file saved as "UTF-8 with BOM" starts with three invisible bytes. Some parsers reject them with "Unexpected token in JSON at position 0" (the token is the invisible character). Save the file as plain UTF-8 or strip the first character. The validator reads such files, so if it validates and your program does not, the BOM is the likely difference.

Still invalid?

Write to support@openanydoc.com and paste the error message and the ten characters around the reported line (no personal data).