JSON Parsing Errors — Common Causes and Fixes
SyntaxError: Unexpected token in JSON is one of the most common errors JavaScript developers hit, usually right after copy-pasting JSON from a log, a Python script, or a hand-edited config file. This guide walks through the most frequent causes of JSON parsing errors and exactly how to fix each one.
1. Trailing Commas
JavaScript object and array literals tolerate a trailing comma. JSON does not.
// Invalid JSON — trailing comma
{
"name": "Priya",
"age": 29,
}
// Error:
// SyntaxError: Unexpected token } in JSON at position 32
// Fix — remove the trailing comma
{
"name": "Priya",
"age": 29
}2. Single Quotes Instead of Double Quotes
JSON strings and keys must use double quotes. Single quotes are valid in JavaScript but not in the JSON spec.
// Invalid JSON
{ 'name': 'Priya', 'city': 'Bengaluru' }
// Fix
{ "name": "Priya", "city": "Bengaluru" }3. Unquoted Object Keys
Bare keys are valid JavaScript but invalid JSON — every key must be a double-quoted string.
// Invalid JSON
{ name: "Priya", age: 29 }
// Fix
{ "name": "Priya", "age": 29 }4. undefined, NaN, and Functions
JSON has no concept of undefined, NaN, Infinity, or functions. These are JavaScript-only values.
// This works in JS but JSON.stringify silently drops the "undefined" key
const obj = { name: "Priya", nickname: undefined };
JSON.stringify(obj); // '{"name":"Priya"}'
// This throws — undefined is not valid JSON text
JSON.parse('{ "name": "Priya", "nickname": undefined }');
// SyntaxError: Unexpected token u in JSON at position 30
// Fix — use null instead
JSON.parse('{ "name": "Priya", "nickname": null }'); // OK5. Comments Inside JSON
JSON does not support // or /* */ comments at all, unlike JSON5 or JSONC used in some config files (e.g. tsconfig.json, which is technically JSONC, not strict JSON).
// Invalid strict JSON
{
"port": 3000 // default port
}
// Fix — remove the comment or use a JSON5 parser if needed
{
"port": 3000
}6. Safely Parsing Untrusted JSON
Always wrap JSON.parse in a try/catch when the input comes from a network request, file, or user input — never assume it will be valid.
function safeParse(text) {
try {
return { ok: true, data: JSON.parse(text) };
} catch (err) {
return { ok: false, error: err.message };
}
}
const result = safeParse('{ "id": 1, }');
if (!result.ok) {
console.error("Invalid JSON:", result.error);
}Quick Checklist Before You Debug Further
- Every key and string value is wrapped in double quotes, not single quotes
- No trailing commas after the last item in an object or array
- No comments anywhere in the JSON text
- No
undefined,NaN, or JavaScript functions — usenullor a number instead - All brackets and braces are properly closed and matched
- Backslashes inside strings are escaped as
\\\\
Frequently Asked Questions
JSON.parse throws "Unexpected token" when the input string is not valid JSON — common causes are trailing commas, single-quoted strings, unquoted object keys, or JavaScript-only values like undefined and NaN that are not valid in JSON.
No. The JSON specification does not allow trailing commas after the last item in an array or object. JavaScript object literals allow them, which is why developers often mistakenly add one when hand-writing JSON.
Yes. Dev Brains AI JSON Formatter validates JSON and highlights the exact location of syntax errors as you paste or type.