A JSON parse error occurs when your application or browser cannot interpret a string as valid JSON. This is one of the most common errors developers encounter when working with APIs, configuration files, or data storage. This guide will help you understand the causes and fix them quickly.
What is a JSON Parse Error?
JSON (JavaScript Object Notation) has strict syntax rules. When data violates these rules, parsers throw a parse error. The error message usually looks like one of these:
SyntaxError: Unexpected token < in JSON at position 0
JSON.parse: unexpected character at line 1 column 1
JSON Parse error: Unrecognized token
ValueError: Expecting property name: line 1 column 2
Common Causes of JSON Parse Errors
1. Receiving HTML Instead of JSON
The most common cause is receiving an HTML error page instead of JSON data:
// This happens when API returns HTML (like a 404 page)
fetch('/api/data')
.then(res => res.json()) // Fails if response is HTML
.catch(err => console.error('Parse error:', err));
// Solution: Check response before parsing
fetch('/api/data')
.then(res => {
if (!res.ok) throw new Error('HTTP error ' + res.status);
const contentType = res.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
throw new Error('Response is not JSON');
}
return res.json();
});
2. Empty Response
// Empty string causes parse error
JSON.parse(''); // SyntaxError
// Solution: Check for empty response
const data = responseText ? JSON.parse(responseText) : null;
3. Single Quotes Instead of Double Quotes
// Invalid: single quotes
{'name': 'John'} // Parse error
// Valid: double quotes
{"name": "John"}
4. Trailing Commas
// Invalid: trailing comma
{"name": "John", "age": 30,}
// Valid: no trailing comma
{"name": "John", "age": 30}
5. Unquoted Keys
// Invalid: unquoted keys
{name: "John"}
// Valid: quoted keys
{"name": "John"}
How to Debug JSON Parse Errors
Use our JSON formatter online to validate and fix your JSON instantly. Paste your JSON into the formatter above and it will highlight exactly where the error is located.
// Debug approach
try {
const data = JSON.parse(jsonString);
} catch (error) {
console.log('Failed to parse:', jsonString.substring(0, 100));
console.log('Error:', error.message);
}
Fixing JSON in Different Languages
Python
import json
try:
data = json.loads(json_string)
except json.JSONDecodeError as e:
print(f"JSON error at line {e.lineno}, column {e.colno}: {e.msg}")
JavaScript
function safeJsonParse(str) {
try {
return { data: JSON.parse(str), error: null };
} catch (e) {
return { data: null, error: e.message };
}
}
Prevention Tips
- Always validate JSON before storing or transmitting
- Use a JSON formatter tool to check syntax
- Set proper Content-Type headers in API responses
- Handle parsing errors gracefully in your code
To quickly validate your JSON data, use our JSON formatter online which shows errors in real-time as you type.