AuraticHub

How to fix common JSON errors

Try it now: JSON Formatter, free and private in your browser.

Open JSON Formatter

JSON looks simple, but the parser is strict. One stray character and the whole document fails. These are the six mistakes behind most errors, with the fix for each.

1. Trailing commas

JavaScript allows a comma after the last item, but JSON does not.

{"a": 1, "b": 2,}   // invalid
{"a": 1, "b": 2}    // valid

2. Single quotes

Strings and keys must use double quotes. Replace every ' with ".

3. Unquoted keys

{name: "Sam"} is valid JavaScript but not valid JSON. Write {"name": "Sam"}.

4. Comments

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

5. Missing or extra brackets

Every { needs a } and every [ needs a ]. Formatting the document with indentation makes the mismatch easy to spot.

6. Values JSON does not support

JSON has no undefined, NaN or Infinity. Use null, a string or a number instead.

Errors that come from other languages

Python prints dictionaries with single quotes and writes True, False and None. That output looks like JSON but is not. Use json.dumps(data) instead of str(data). In JavaScript, JSON.stringify silently drops keys whose value is undefined, so a field can vanish without an error.

Copy and paste problems

Text copied from Word, Slack, Notion or a web page often contains "smart quotes" that look right but are different characters from the straight " JSON needs. A hidden byte order mark at the start of a file can also break parsing. If everything looks correct and the parser still fails, retype the quotes or paste the text through a plain-text editor first.

How to avoid errors in the first place

  • Generate JSON with JSON.stringify or json.dumps instead of building strings by hand.
  • Use an editor with JSON validation, such as VS Code, which underlines mistakes as you type.
  • Validate API responses before you use them, and handle a parse failure instead of assuming success.
  • For config files that need comments, consider JSON5 or YAML, and convert to JSON at build time.

A faster way to find the problem

Paste your JSON into the formatter. If it is invalid, the error names the position where parsing stopped, which is usually right after the real mistake. Look at the character just before that point.

FAQ

What does "Unexpected token" mean?
The parser found a character it did not expect, often a trailing comma, a quote or a stray symbol.
Can JSON have comments?
No. Strip them out first, or use a format such as JSON5 if you control both ends.
Why does my JSON work in JavaScript but fail in a parser?
JavaScript object literals are more relaxed than JSON. They allow unquoted keys, single quotes and trailing commas. JSON allows none of them.
Is JSON case sensitive?
Yes. true, false and null must be lowercase.

Keep reading