DEV Community

Snappy Tools
Snappy Tools

Posted on

5 Common JSON Errors and How to Fix Them Fast

JSON is everywhere — API responses, config files, data pipelines. And JSON errors have a habit of appearing at the worst moments. Here are the five most common JSON syntax errors, why they happen, and how to fix them.

1. Trailing Comma

Error: Unexpected token } or Unexpected token ]

//  Invalid
{
  "name": "Alice",
  "age": 30,
}

//  Valid
{
  "name": "Alice",
  "age": 30
}
Enter fullscreen mode Exit fullscreen mode

Why it happens: JavaScript object literals allow trailing commas, so developers copy-paste JS code into JSON. JSON (defined by RFC 7159) does not allow them.

Fix: Remove the comma after the last key-value pair or last array item.


2. Single Quotes Instead of Double Quotes

Error: Unexpected token ' or JSON.parse() failing silently

//  Invalid
{ 'name': 'Alice' }

//  Valid
{ "name": "Alice" }
Enter fullscreen mode Exit fullscreen mode

Why it happens: JavaScript string literals accept single or double quotes, but JSON requires double quotes — for both keys and string values.

Fix: Replace all single quotes with double quotes. If your string contains a double quote, escape it: "She said \"hello\"".


3. Unquoted Keys

Error: Unexpected token n (or whatever letter starts the unquoted key)

//  Invalid
{ name: "Alice" }

//  Valid
{ "name": "Alice" }
Enter fullscreen mode Exit fullscreen mode

Why it happens: In JavaScript, object keys don't need quotes unless they contain special characters. In JSON, every key must be a double-quoted string.

Fix: Wrap every key in double quotes.


4. Comments in JSON

Error: Unexpected token /

//  Invalid
{
  // This is the user's name
  "name": "Alice"
}

//  Valid
{
  "name": "Alice"
}
Enter fullscreen mode Exit fullscreen mode

Why it happens: Developers used to writing YAML, TOML, or JavaScript configs (which all support comments) assume JSON does too. Douglas Crockford removed comments from JSON intentionally to prevent directives being added to config files.

Fix: Remove all comments. If you need comments in a config file, consider JSON5, JSONC (used by VS Code), or YAML as your format.


5. Wrong Boolean / Null Casing

Error: Unexpected token T or Unexpected token N

//  Invalid
{ "active": True, "data": None, "count": NaN }

//  Valid
{ "active": true, "data": null, "count": 0 }
Enter fullscreen mode Exit fullscreen mode

Why it happens: Python uses True, False, and None. JSON uses lowercase true, false, and null. There is no NaN or undefined in JSON — use null instead.

Fix: Replace Python booleans/None with JSON lowercase equivalents. For NaN and Infinity, use null or a sentinel value like -1.


Quick Diagnosis: Use an Online Formatter

The fastest way to find any JSON error is to paste your JSON into an online formatter that shows the exact line and character position of the problem.

SnappyTools JSON Formatter validates and prettifies JSON instantly in your browser — no server, no signup. If there's a syntax error, it highlights exactly where the parser failed, which is far more useful than SyntaxError: Unexpected token alone.


Cheat Sheet

Issue Invalid Valid
Trailing comma {"a":1,} {"a":1}
Single quotes {'k':'v'} {"k":"v"}
Unquoted key {k:1} {"k":1}
Comment {"a":1 // note} {"a":1}
Python bool {"ok": True} {"ok": true}
Python None {"x": None} {"x": null}

Bookmark this cheat sheet and you'll spend far less time hunting JSON parse errors.

Top comments (0)