5 reasons your JSON fails to parse (and how to fix each one)

2026-11-14来源 dev.toEnglish

If a parser throws Unexpected token, the cause is almost always one of five things.

1. Trailing comma

JSON does not allow a comma after the last item, even though JavaScript does.

{ "name": "Ana", "age": 30, } 

Remove the last comma.

2. Single quotes

Keys and strings must use double quotes. {'name': 'Ana'} is a JavaScript object, not valid JSON.

3. Unquoted keys

{name: "Ana"} fails. Every key needs double quotes.

4. Comments

Standard JSON has no comment syntax. Remove // and /* */ lines before parsing.

5. Invisible characters at the start

A BOM or a pasted line number can break the first token. If the error points to position 0 and the file looks fine, check for this.

Finding the exact spot

Paste the data into a formatter that reports the line and column of the error, then fix that one place and re-check. If the data comes from a log or a Python print, a repair step that fixes quotes and trailing commas saves a lot of manual editing.

We built a free formatter and a repair tool for this. Both run in your browser, so your data is not uploaded:

Disclosure: I work on SnipFerry.

原文:https://www.a8q9.cn/en/article/5-reasons-your-json-fails-to-parse-and-how-to-fix-each-one-f32484.html

相关文章