JSON is supposed to be simple. It’s lightweight, human-readable, and supported by every modern programming language. Yet JSON parsing errors remain one of the most common and frustrating issues developers face. You look at the JSON, it looks fine, but your parser insists it’s invalid. Or worse, it works in one environment but fails in another.
This guide breaks down why JSON fails to parse, what’s actually happening behind the scenes, and how to fix these issues quickly and reliably.
1. The Problem: "It Looks Like Valid JSON… Why Is It Breaking?"
JSON parsing errors usually show up in a few predictable but unhelpful ways:
- "Unexpected token" errors
- "Unexpected end of JSON input"
- "Invalid character at position 0"
- Server returns HTML instead of JSON
- JSON works locally but fails in production
Real-World Examples
// Error: Unexpected token }
// Usually caused by a missing comma
// Error: Unexpected token at position 0
// Caused by a UTF-8 BOM
// Error: Unexpected token '
// Caused by single quotes instead of double quotes2. The Cause: Strict Syntax, Hidden Characters, and Wrong Assumptions
2.1 JSON Is Not JavaScript
JSON looks like JavaScript, but it is far more strict. Things allowed in JS objects are NOT allowed in JSON:
❌ No Single Quotes
// JavaScript (valid)
{ 'name': 'Bill' }
// JSON (invalid)
{ 'name': 'Bill' }
// JSON (correct)
{ "name": "Bill" }❌ No Trailing Commas
// JavaScript (valid)
{ name: "Bill", }
// JSON (invalid)
{ "name": "Bill", }❌ No Comments
// JSON (invalid)
{ // comment
"name": "Bill"
}JSON vs JavaScript Comparison
| Feature | JavaScript | JSON |
|---|---|---|
| Quotes | Single or double | Double only |
| Keys | Unquoted allowed | Must be quoted |
| Trailing commas | Allowed | Not allowed |
| Comments | Allowed | Not allowed |
| undefined | Allowed | Not allowed |
2.2 Structural Errors
Missing Commas
// ❌ Wrong
{
"name": "Bill"
"age": 30
}
// ✅ Correct
{
"name": "Bill",
"age": 30
}Unescaped Quotes
// ❌ Wrong
{ "msg": "He said "hello"" }
// ✅ Correct
{ "msg": "He said \"hello\"" }2.3 Encoding Issues (UTF-8, BOM, Hidden Characters)
Encoding issues are one of the most overlooked causes of JSON parsing failures. A JSON file may contain:
- A UTF-8 BOM at the beginning
- Smart quotes copied from Word or Slack
- Non-breaking spaces
- Invisible Unicode characters
These characters are invisible but fatal.
2.4 Copy-Paste Corruption
Copying JSON from PDFs, chat apps, or documents often introduces invalid characters. Always paste into a plain-text editor first.
2.5 Double Encoding
"{\"name\":\"Bill\"}"This is a JSON string containing JSON—not valid JSON.
3. The Solution: A Reliable Workflow
3.1 Step 1: Format the JSON
Formatted JSON makes structural errors obvious.
// JavaScript
JSON.stringify(JSON.parse(str), null, 2);3.2 Step 2: Validate with a Strict Parser
try {
JSON.parse(str);
} catch (e) {
console.error(e.message);
}3.3 Step 3: Check Encoding
- Remove BOM
- Replace smart quotes
- Remove hidden characters
3.4 Step 4: Fix Syntax Using a Checklist
- Keys in double quotes?
- No trailing commas?
- Braces and brackets matched?
- Quotes escaped?
- UTF-8 without BOM?
3.5 Step 5: Verify the Raw Input
If you see Unexpected token <, you're parsing HTML, not JSON.
3.6 Step 6: Use JSON Schema
{
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"]
}3.7 Step 7: Automate Formatting
// .prettierrc
{
"tabWidth": 2,
"trailingComma": "none"
}Conclusion
JSON parsing errors feel mysterious, but they’re almost always caused by a small set of predictable issues: strict syntax rules, hidden characters, encoding problems, and copy-paste corruption. Follow the workflow in this guide to eliminate most JSON-related bugs before they reach production.