How to fix invalid JSON

Unexpected token, unexpected end of input, bad control character: what each JSON error actually means, how to find the line it is on and how to fix it.

Last updated

JSON is strict. One stray comma, one single quote, one missing bracket and the whole document is rejected, often with a message that names a character and a position but not a line you can find. The good news is that nearly every JSON error is one of a handful of mistakes.

Find the line first

“Unexpected token at position 4317” isn’t something you can act on in a long file. Paste the JSON into the JSON formatter and it shows the line and column, the line itself with a marker under the problem and a plain explanation such as “trailing comma: remove the comma before ]”. The same message appears in every browser, since browsers word these errors differently and some give no position at all.

The common mistakes

  • Trailing comma. [1, 2, 3,] or a comma after the last property. Remove it.
  • Single quotes. JSON strings and keys need double quotes.
  • Unquoted keys. {name: "Ada"} is JavaScript, not JSON. Quote the key.
  • Comments. // and /* */ aren’t allowed in JSON.
  • Missing comma between items, usually after editing by hand.
  • A line break inside a string. Write it as \n.
  • Leading zeros, such as 007. Numbers can’t start with zero; make it a string.

Turning on “fix common mistakes” in the formatter corrects most of these automatically and tells you which it relaxed, while still reporting where the original went wrong.

When the error is at position 0

If the very first character is wrong, the text was probably not JSON at all. < at position 0 means HTML arrived instead, typically an error or login page from a server. An empty response gives “unexpected end of input”. In both cases the fix is in whatever produced the response, not in the JSON.

Numbers that change

A subtler problem: the JSON is valid, but a large ID comes back different. Integers above about nine quadrillion lose precision in JavaScript, so 12345678901234567890 becomes 12345678901234567000. The formatter keeps those digits exactly and warns you. It is the JSON cousin of the spreadsheet problem in why Excel removes leading zeros.

Turning JSON into a table

Once it is valid, a list of records can go straight into a spreadsheet with JSON to CSV and back again with CSV to JSON. For very large exports, split the CSV before importing it.

Common questions

What does Unexpected token < in JSON at position 0 mean?

The program expected JSON and received HTML instead, which starts with a < character. It is almost always an error page, a login page or a 404 returned by a server in place of the data. Look at the raw response to see what actually arrived.

What does Unexpected end of JSON input mean?

The text stopped before the JSON was complete. Usually the response was empty or it was cut off or a closing bracket is missing at the end of a file.

Can JSON have comments or trailing commas?

Standard JSON allows neither. Many configuration files use a relaxed variant that does, which is why a file that works in one tool fails in another. Removing them makes it valid everywhere.

Why did my ID number change after parsing?

JavaScript and many other languages store JSON numbers as 64-bit floats, which hold integers exactly only up to 9,007,199,254,740,991. Larger IDs are silently rounded. Sending such IDs as strings avoids the problem.

Tools for this

More guides