SyntaxError: Unexpected token < in JSON at position 0
json.decoder.JSONDecodeError: Expecting value: line 1 column 1
JSON.parse: unexpected character at line 1 column 1
The string you’re trying to parse as JSON isn’t valid JSON.
Fix 1: Response Is HTML, Not JSON
// ❌ API returned an error page (HTML) instead of JSON
const res = await fetch('/api/data');
const data = await res.json(); // 💥 Unexpected token '<'
// ✅ Check response before parsing
const res = await fetch('/api/data');
const text = await res.text();
console.log(text); // See what you actually got
const data = JSON.parse(text);
Fix 2: Empty Response
// ❌ Response body is empty
const data = JSON.parse(''); // 💥
// ✅ Check for empty response
const text = await res.text();
const data = text ? JSON.parse(text) : null;
Fix 3: Trailing Commas
// ❌ JSON doesn't allow trailing commas
{ "name": "Alice", "age": 30, }
// ✅ Remove trailing comma
{ "name": "Alice", "age": 30 }
Fix 4: Single Quotes
// ❌ JSON requires double quotes
{ 'name': 'Alice' }
// ✅ Use double quotes
{ "name": "Alice" }
Fix 5: Unescaped Characters
// ❌ Newlines and tabs must be escaped
{ "bio": "Line 1
Line 2" }
// ✅ Escape special characters
{ "bio": "Line 1\nLine 2" }
Fix 6: Python — Wrong Quotes in String
# ❌ Using Python dict syntax instead of JSON
import json
data = json.loads("{'name': 'Alice'}") # 💥 Single quotes
# ✅ Use double quotes
data = json.loads('{"name": "Alice"}')
# Or convert Python dict to JSON
import json
data = {"name": "Alice"}
json_str = json.dumps(data)
Debugging
// Validate JSON before parsing
try {
const data = JSON.parse(text);
} catch (e) {
console.log('Invalid JSON:', text.substring(0, 200));
}
# Validate a JSON file
python3 -m json.tool file.json
# Or use jq
cat file.json | jq .