Menu

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

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

DEV Community: javascript·AZ Tech Era Tools·2 days ago
#PLmI2kvI
#devtojavascript#devto#dev#json#quotes
Reading 0:00
15s threshold

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 , }

Enter fullscreen mode

Exit fullscreen mode

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.