The Complete JSON Error Cheat Sheet
Every JSON parse error falls into one of about a dozen categories. Match your error message to the section below to find the fix.
Error: "Unexpected token , in JSON at position N"
Cause: a comma where the parser expected a value or the end of the structure. Almost always a trailing comma.
// BROKEN
{
"colors": ["red", "green", "blue",]
}
// FIXED — remove the trailing comma
{
"colors": ["red", "green", "blue"]
}Error: "Unexpected token ' in JSON at position N"
Cause: single quotes used instead of double quotes.
// BROKEN
{ 'name': 'Alice', 'age': 30 }
// FIXED — replace ' with "
{ "name": "Alice", "age": 30 }Error: "Unexpected token n in JSON at position N" (unquoted key)
Cause: a JavaScript-style object literal with unquoted keys.
// BROKEN (JavaScript, not JSON)
{ name: "Alice", age: 30 }
// FIXED — wrap keys in double quotes
{ "name": "Alice", "age": 30 }Error: "Unexpected end of JSON input"
Cause: a bracket is missing, so the parser ran out of input while still expecting content.
// BROKEN — missing closing }
{
"user": {
"name": "Alice"
}
// FIXED
{
"user": {
"name": "Alice"
}
}Error: "Bad control character in string literal"
Cause: a raw newline, tab, or other control character inside a string.
// BROKEN — literal newline inside string
{
"message": "hello
world"
}
// FIXED — use the \n escape sequence
{
"message": "hello\nworld"
}Error: "Unexpected token / in JSON at position N"
Cause: comments — JSON does not allow // or /* */ comments.
// BROKEN
{
"port": 8080, // web server port
"debug": true
}
// FIXED — remove all comments
{
"port": 8080,
"debug": true
}
// NOTE: If you need comments, use JSONC (VS Code settings) or YAML.Error: "Unexpected non-whitespace character after JSON"
Cause: extra text after the top-level closing bracket.
// BROKEN — trailing character
{ "name": "Alice" };
// FIXED — remove the semicolon
{ "name": "Alice" }Programmatic Fixes for Common Issues
Strip trailing commas (safe regex)
function stripTrailingCommas(json) {
return json.replace(/,(\s*[}\]])/g, '$1');
}
const broken = '{"a": 1, "b": [2, 3,],}';
const fixed = stripTrailingCommas(broken);
JSON.parse(fixed); // { a: 1, b: [2, 3] }Use JSON5 to accept lenient input
import JSON5 from 'json5';
// JSON5 accepts trailing commas, single quotes, unquoted keys, comments
const lenient = `{
name: 'Alice', // JavaScript-style
age: 30,
}`;
const obj = JSON5.parse(lenient);
const strict = JSON.stringify(obj, null, 2);
// {
// "name": "Alice",
// "age": 30
// }Parse JSONC (JSON with Comments)
import { parse } from 'jsonc-parser';
const jsonc = `{
// A comment
"name": "Alice"
}`;
const obj = parse(jsonc); // { name: "Alice" }When to Look Beyond the Reported Position
Sometimes the reported error position isn't where the actual problem lives. A missing quote earlier in the file can push the parser far past the real bug before it finally gives up.
Common cases:
- Missing closing quote in a string — the parser treats everything after the opening
"as string content until it finds the next unescaped", which could be many lines later. Search backwards from the error position for a suspicious quote. - Unbalanced bracket — one missing
}or]pushes the error to the end-of-input. Use an editor with bracket highlighting to find the mismatch. - Truncated input — if the JSON was copied via a terminal or a limited paste buffer, only a fraction may have made it. Compare byte counts with the source.
Fixing JSON at Scale (Batch Repair)
If you have hundreds of broken JSON files (from a bad migration or a legacy export), batch-fix them with a script:
# Python — attempt to parse each file, log which fail
import json, glob, sys
for path in glob.glob('**/*.json', recursive=True):
try:
with open(path) as f:
json.load(f)
except json.JSONDecodeError as e:
print(f'{path}: line {e.lineno} col {e.colno}: {e.msg}')For files where the fix is mechanical (trailing commas, single quotes), automate:
# Node.js — use JSON5 to reparse and re-emit as strict JSON
import fs from 'fs';
import glob from 'glob';
import JSON5 from 'json5';
for (const file of glob.sync('**/*.json')) {
try {
const obj = JSON5.parse(fs.readFileSync(file, 'utf8'));
fs.writeFileSync(file, JSON.stringify(obj, null, 2));
console.log('Fixed:', file);
} catch (e) {
console.error('Could not fix:', file, e.message);
}
}Preventing Invalid JSON in the First Place
- Always serialise with JSON.stringify — never build JSON by concatenating strings; the serialiser handles escaping correctly.
- Add a pre-commit hook — run
jq emptyon all .json files before every commit. - Validate in CI — fail the build if any JSON is invalid. See our JSON Validator for pipeline examples.
- Use TypeScript — with proper types, most JSON-building bugs become compile-time errors.
- Test with malformed input — your API should fail gracefully on invalid JSON, not crash.
Key Facts
- Diagnoses:
- All standard JSON parse errors — trailing commas, quotes, brackets, escape sequences
- Position info:
- Precise character offset for every error
- Auto-fix:
- Manual repair — the tool guides you, you edit
- Privacy:
- Fully in-browser — nothing uploaded anywhere
- Best for:
- API responses, config files, log payloads, hand-written JSON
- Cost:
- Free, no signup, no ads on the tool
Related JSON Tools
- Validate JSON Online — check whether JSON is valid without repair guidance
- JSON Lint Online — JSONLint-style error reporting
- Format JSON Online — beautify JSON with proper indentation
- JSON Viewer Online — explore valid JSON structure
- Escape JSON String — safely embed strings with special characters
- Unescape JSON String — reverse an escaped JSON string