7 Common JSON Errors and How to Fix Them
Trailing commas, single quotes, unquoted keys — here are the 7 most common JSON syntax errors developers make and exactly how to fix each one.
- json
- debugging
- errors
- syntax
title: "7 Common JSON Errors and How to Fix Them" description: "Trailing commas, single quotes, unquoted keys — here are the 7 most common JSON syntax errors developers make and exactly how to fix each one." date: "2026-06-02" tags: ["json", "debugging", "errors", "syntax"] relatedTool: "json-formatter" published: true
JSON is strict. Unlike JavaScript objects, JSON has rigid syntax rules — and the error messages parsers throw are often unhelpful (SyntaxError: Unexpected token } doesn't tell you which }). The exact wording also varies between parsers: V8, Python's json module, and Go's encoding/json all phrase the same problem differently. Below are the seven most common JSON mistakes, why they happen, and the exact fix for each. If you've got a broken payload right now, paste it into the CodeScrub JSON Formatter and it'll point you at the exact line and character where the parser choked.
1. Trailing Comma After the Last Item
Broken:
{
"name": "Ada",
"role": "admin",
}
Error: SyntaxError: Unexpected token }
Fix: remove the comma after the last value.
{
"name": "Ada",
"role": "admin"
}
Why it happens: JavaScript allows trailing commas, JSON does not. This is the single most common mistake when editing JSON by hand. Looser relatives — JSON5, JSONC, HJSON — do accept trailing commas, but the standard JSON.parse, json.loads, and encoding/json you actually run in production will refuse them.
2. Single Quotes Instead of Double Quotes
Broken:
{
'name': 'Ada'
}
Error: SyntaxError: Unexpected token '
Fix: JSON requires double quotes around both keys and string values.
{
"name": "Ada"
}
Why it happens: JavaScript and Python both accept single quotes for strings, so the muscle memory carries over. JSON is not JavaScript. If your "JSON" is actually the output of a Python print(my_dict) or a JavaScript console.log of an object, you'll see single quotes — those are debug representations, not valid JSON. Always serialize through json.dumps or JSON.stringify before treating the result as JSON.
3. Unquoted Keys
Broken:
{
name: "Ada",
role: "admin"
}
Error: SyntaxError: Unexpected token n
Fix: every key must be a double-quoted string.
{
"name": "Ada",
"role": "admin"
}
Why it happens: JavaScript object literals don't require quoted keys. JSON does — always.
4. Comments in JSON
Broken:
{
"name": "Ada",
// This is the user's role
"role": "admin"
}
Error: SyntaxError: Unexpected token /
Fix: JSON does not support comments — not //, not /* */. Remove them entirely.
{
"name": "Ada",
"role": "admin"
}
Workaround: if you need comments in config files, use JSONC (JSON with Comments), which is what VS Code's settings.json and TypeScript's tsconfig.json actually use. Standard JSON parsers will still reject it.
Why it happens: developers used to YAML, Python, or JavaScript expect comments to work, and config-file conventions (TOML, YAML, dotenv) all support them. JSON was designed as a wire format for machines, not a config format for humans, so comments were left out by design.
5. Missing Commas Between Items
Broken:
{
"name": "Ada"
"role": "admin"
}
Error: SyntaxError: Unexpected string or Expected ',' or '}'
Fix: every key-value pair needs a trailing comma except the last one.
{
"name": "Ada",
"role": "admin"
}
Why it happens: when you append a new field to an existing object, it's easy to forget to add a comma to the previous line. Turn on JSON linting in your editor (VS Code does it automatically; JetBrains IDEs need the bundled JSON plugin enabled) and this class of bug disappears entirely.
6. Using undefined, NaN, or Infinity
Broken:
{
"name": "Ada",
"score": NaN,
"callback": undefined
}
Error: SyntaxError: Unexpected token N or Unexpected token u
Fix: JSON only supports six value types — string, number, boolean (true / false), null, object, and array. undefined, NaN, Infinity, and -Infinity are JavaScript concepts that don't exist in JSON. Replace them with null, a sentinel number, or omit the field entirely.
{
"name": "Ada",
"score": null,
"callback": null
}
Why it happens: JSON.stringify silently drops undefined from objects and converts NaN/Infinity to null, so the problem usually shows up only when you hand-edit JSON or build it from a template.
7. Unescaped Special Characters in Strings
Broken:
{
"path": "C:\new\test",
"message": "She said "hello""
}
Error: various "unexpected token" errors, depending on the parser.
Fix: escape backslashes and embedded double quotes.
{
"path": "C:\\new\\test",
"message": "She said \"hello\""
}
Characters that must be escaped inside JSON strings: \" (double quote), \\ (backslash), \n (newline), \t (tab), and \r (carriage return). Forward slashes (/) may optionally be escaped as \/ but it's not required.
Why it happens: when you hand-build JSON inside another string (a shell command, a SQL INSERT, a template), every backslash needs to be doubled to survive both layers of parsing. Use a JSON library's stringify function whenever you can — it handles escaping correctly so you don't have to think about it.
Quick Debugging Checklist
When the parser is unhappy and you don't know why, the line-and-column number in the error is usually accurate but the message rarely is. Work through this list top to bottom and you'll catch the cause within a minute:
- Paste your JSON into the CodeScrub JSON Formatter to find the exact error line and column.
- Check for trailing commas — by far the most common mistake.
- Replace any single quotes with double quotes.
- Make sure every key is double-quoted.
- Remove any
//or/* */comments. - Replace
undefined,NaN, andInfinitywithnullor a real value. - Escape backslashes and quotes inside string values.
- Verify brackets and braces are balanced — every
{has a matching}, every[has a matching].
Bottom line
Most JSON errors come from confusing JSON with JavaScript. Remember: JSON is stricter — always double-quoted, no comments, no trailing commas, and no undefined or NaN. If you're producing JSON programmatically, always use a real library (JSON.stringify, json.dumps, encoding/json) rather than concatenating strings; nine out of ten of these errors disappear the moment you do. When you're stuck on hand-written JSON, paste the payload into a formatter and let the parser tell you exactly what's wrong instead of guessing.