You are deep in the zone, building a new feature or connecting to a critical API, when suddenly everything stops working. Your console flashes: SyntaxError: JSON.parse: unexpected character. JSON is the backbone of modern web communication — but also extremely strict. One misplaced character can bring your entire application to a halt.
Why JSON Parse Errors Happen
Parsing JSON means transforming a plain text string into a JavaScript object. The parser reads every character in sequence and expects each one to follow precise rules. If any character violates the JSON specification, the parser stops immediately and throws an error — it does not try to recover or guess your intent the way a browser does with HTML.
The Most Common Causes
1. Wrong Quote Type
JSON requires double quotes for all keys and string values. Single quotes, backticks, or no quotes at all will all cause a parse error.
// ❌ All invalid JSON
{ name: "Alex" } // unquoted key
{ 'name': "Alex" } // single-quoted key
{ "name": 'Alex' } // single-quoted value
// ✅ Valid JSON
{ "name": "Alex" }
2. Trailing Comma
Unlike JavaScript objects, JSON does not allow a comma after the last property or array item. This is one of the most frequent mistakes when writing JSON by hand.
// ❌ Invalid — trailing comma
{
"name": "Alex",
"age": 30,
}
// ✅ Valid
{
"name": "Alex",
"age": 30
}
3. Unescaped Special Characters in Strings
If a string contains a double quote, backslash, or control characters, they must be escaped. A bare double quote inside a JSON string will end the string early and break parsing.
// ❌ Invalid — unescaped quote
{ "message": "He said "Hello" to me" }
// ✅ Valid — escaped quote
{ "message": "He said \"Hello\" to me" }
4. Incorrect Data Types
JSON has strict type rules. Booleans must be lowercase, numbers must not be quoted, and null (not None or nil) is the null value.
// ❌ Invalid types
{ "active": True, "score": "42", "data": None }
// ✅ Valid types
{ "active": true, "score": 42, "data": null }
5. Invisible Characters and Smart Quotes
Copy-pasting from Word documents, PDFs, or certain chat applications can introduce invisible Unicode characters or "smart quotes" (curly quotes like " and ") that look correct but are not standard ASCII double quotes. The JSON parser rejects them.
Step-by-Step Debugging Strategy
- Read the error position. The console error message includes a line and column number. The actual mistake is usually at or just before that position.
- Log the raw string. Before calling
JSON.parse(), log the exact string. This reveals if it isundefined, truncated, or contains unexpected characters. - Check quotes. Search for any single quotes, smart quotes, or unquoted keys in your JSON.
- Hunt for trailing commas. Check the last item in every object and array.
- Validate in a tool. Paste the JSON into our validator — it instantly highlights the line and character where the error occurs.
Defensive Parsing Pattern
function safeParse(raw) {
if (!raw || typeof raw !== 'string') {
console.warn('safeParse: input is not a string', raw);
return null;
}
try {
return JSON.parse(raw);
} catch (err) {
console.error('JSON parse failed:', err.message);
console.log('Raw input was:', raw.slice(0, 200)); // log first 200 chars
return null;
}
}
This pattern guards against the three most common runtime issues: undefined input, non-string input, and malformed JSON. The truncated log helps you identify the problem without flooding the console with huge payloads.
Validate & Fix JSON Instantly
Paste your broken JSON into our free validator and get a clear error location in seconds.
Open JSON ValidatorDebugging should not be frustrating. With the right tools and a clear understanding of JSON rules, most parse errors become a five-second fix. The key is to always validate your data at the boundary — right where it enters your application from a network request, a file, or user input.