Skip to main content

← Back to blog

Common JSON Errors and How to Fix Them

Almost every “Unexpected token in JSON” error comes from the same short list of mistakes. Here they are, with the fix for each. (New to the format? Start with What is JSON.)

These are the messages our own tool prints. We fed six broken snippets into the JSON Formatter and recorded exactly what came back, so you can match the message you are staring at:

  • {"a":1,} → Expected double-quoted property name in JSON at position 7 (line 1 column 8)
  • {'a':1} and {a:1} → Expected property name or ’}’ in JSON at position 1 (line 1 column 2)
  • {"a":{"b":1} → Expected ’,’ or ’}’ after property value in JSON at position 12 (line 1 column 13)
  • {"a":undefined} → Unexpected token ‘u’, ”{“a”:undefined}” is not valid JSON
  • {"a":1 "b":2} → Expected ’,’ or ’}’ after property value in JSON at position 7

Two things worth knowing. Single-quoted strings and unquoted keys produce the same message, because the parser gives up at the same spot either way. And the wording comes from the browser’s own JSON parser, so Firefox and Safari phrase it differently — the position numbers are the part you can rely on. Our tool adds the line and column itself.

1. Trailing commas

JSON does not allow a comma after the last item:

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

2. Single quotes

Strings and keys must use double quotes:

{ 'name': 'Ann' }   ← invalid
{ "name": "Ann" }   ← valid

3. Unquoted keys

Unlike JavaScript objects, JSON keys must be quoted:

{ name: "Ann" }     ← invalid
{ "name": "Ann" }   ← valid

4. Comments

JSON has no comments. Remove // and /* */ before parsing.

5. Wrong or missing brackets

Every { needs a } and every [ a ]. A common one is mixing them up or forgetting to wrap multiple records in an array [ … ].

A missing bracket is the one error whose reported position is furthest from the mistake. {"a":{"b":1} — one closing brace short — gives

✗ JSON syntax error: Expected ',' or '}' after property value in JSON at position 12 (line 1 column 13) [L1:13]

Position 12 is the end of the input, because the parser only knows something is missing once it runs out of characters to read. The mistake is at the opening { on the left; the report is at the far right. Compare a missing comma, {"a":1 "b":2}, which gives

✗ JSON syntax error: Expected ',' or '}' after property value in JSON at position 7 (line 1 column 8) [L1:8]

Same message, but position 7 lands exactly where the comma should have been. When the reported column is the last character of the file, look for an unclosed bracket rather than reading the line it points at.

6. Not-a-number values

NaN, Infinity and undefined aren’t valid JSON. Use a number, a string, or null.

Not-a-number values fail differently again, and the message says so. {"a":undefined} gives

✗ JSON syntax error: Unexpected token 'u', "{"a":undefined}" is not valid JSON

Notice the parser quotes your whole input back rather than giving a column: it could not even begin to interpret undefined as a value, so there is no position where a valid token went wrong. NaN and Infinity fail the same way. JSON has exactly one way to say “no value” and that is null.

The fastest fix: validate

Rather than hunt by eye, paste your JSON into a validator that points to the exact error location:

Fix the six mistakes above and the vast majority of “invalid JSON” problems disappear — everything runs locally in your browser, so even sensitive payloads stay private.

A real example, before and after

Here is a payload that trips over three of the mistakes above at once:

{
  'user': "Ann",
  "roles": ["admin", "editor",],
  "active": True,
}

A parser stops at the first problem it meets and reports something terse like Unexpected token ' at position 4, even though there are three separate issues. Fix them together — swap the single quotes for double quotes, drop the two trailing commas, and lower-case the boolean:

{
  "user": "Ann",
  "roles": ["admin", "editor"],
  "active": true
}

Watch out for True, False and None: those are Python spellings. JSON only knows lowercase true, false and null.

JSON is not a JavaScript object

Most of these mistakes come from treating JSON as if it were JavaScript source code. It isn’t — JSON is a text format with a stricter grammar:

  • keys are always quoted strings, never bare identifiers;
  • only straight double quotes count — not single quotes, backticks, or the curly “smart quotes” a word processor inserts;
  • no trailing commas, comments, or function calls;
  • numbers can’t have a leading +, a leading zero, or a bare trailing dot.

When you can, build JSON with a real serializer — JSON.stringify in JavaScript, json.dumps in Python — instead of gluing strings together by hand. It gets the quoting and escaping right every time.

FAQ

Why does the error say “position 0”?

The input is empty, starts with a byte-order mark (BOM), or is actually an HTML error page rather than JSON. Check what your API really returned before blaming the syntax.

Can I keep comments in a config file?

Not in plain JSON. Use JSON5 or JSONC while editing, or switch to YAML, then strip anything non-standard before a strict parser sees it.

The file is huge and won’t open — now what?

Format it first so the nesting becomes visible, then collapse the branches you don’t need. Pasting it into the JSON formatter does both in one step.