JSON Parse Error: How to Fix All Common Errors (Complete Guide)
๐ง Table of Contents
If you've ever worked with JSON data in JavaScript, Python, or any other language, you've probably
encountered the dreaded JSON parse error. These errors can be frustrating because they
often provide cryptic messages like Unexpected token or
Unexpected end of JSON input without telling you exactly what's wrong.
In this comprehensive guide, we'll explore every type of JSON parse error you might encounter, explain exactly what causes each one, and show you how to fix them with real code examples. Whether you're a beginner or an experienced developer, this guide will help you become a JSON debugging expert.
Why JSON Errors Happen
JSON (JavaScript Object Notation) has strict syntax rules. Unlike JavaScript objects, JSON doesn't allow trailing commas, single quotes, comments, or unquoted property names. Even a single misplaced character will cause a parse error.
What is JSON Parse Error?
A JSON parse error occurs when you try to convert a string into a JavaScript object
using JSON.parse(), but the string doesn't contain valid JSON syntax. The JavaScript engine
can't understand the data structure, so it throws an error.
// Trying to parse invalid JSON
const badJSON = '{ name: "Alice", age: 25 }'; // Missing quotes around "name"
try {
const data = JSON.parse(badJSON);
} catch (error) {
console.log(error.message);
// Output: "Unexpected token n in JSON at position 2"
}
The error message tells us there's an "Unexpected token n" at position 2. This is because
name should be "name" in valid JSON โ all property names must be double-quoted
strings.
Most Common JSON Parse Errors
Here are the top 5 JSON parse errors that developers encounter most frequently:
- Unexpected token <X> โ Invalid character found where it shouldn't be
- Unexpected end of JSON input โ JSON is incomplete (missing brackets)
- Invalid JSON โ General syntax error
- Trailing comma error โ Comma after the last element
- Single quote error โ Using ' instead of "
Let's dive deep into each error type with examples and solutions.
Error #1: Unexpected Token
The "Unexpected token" error is the most common JSON parse error. It means the parser found a character that doesn't belong where it was found.
Error Message
SyntaxError: Unexpected token X in JSON at position Y
Common Causes:
1. Unquoted Property Names
{
name: "Alice",
age: 25
}
{
"name": "Alice",
"age": 25
}
2. Comments in JSON
{
"name": "Alice",
// This is a comment
"age": 25
}
{
"name": "Alice",
"age": 25
}
3. Undefined or NaN Values
{
"value": undefined,
"number": NaN
}
{
"value": null,
"number": 0
}
Error #2: Unexpected End of JSON Input
This error occurs when your JSON string is incomplete โ usually because of missing closing brackets or braces.
Error Message
SyntaxError: Unexpected end of JSON input
Common Causes:
1. Missing Closing Bracket
{
"name": "Alice",
"skills": ["JavaScript", "Python"]
// Missing } here
{
"name": "Alice",
"skills": ["JavaScript", "Python"]
}
2. Parsing Empty String
// This will throw "Unexpected end of JSON input"
const data = JSON.parse("");
const jsonString = getDataFromAPI();
if (jsonString && jsonString.trim()) {
const data = JSON.parse(jsonString);
} else {
console.log("No JSON data received");
}
Error #3: Invalid JSON Format
Sometimes JSON can have structural problems that make it completely invalid.
Multiple Root Elements
{"name": "Alice"} {"name": "Bob"}
[
{"name": "Alice"},
{"name": "Bob"}
]
Error #4: Trailing Comma
Unlike JavaScript, JSON doesn't allow trailing commas after the last element in arrays or objects.
{
"name": "Alice",
"age": 25, // This comma breaks JSON!
}
{
"name": "Alice",
"age": 25
}
Pro Tip: Finding Trailing Commas
Use our JSON Formatter to automatically detect and highlight trailing comma locations with exact line numbers!
Error #5: Single Quotes Instead of Double Quotes
JSON requires double quotes for all strings. Single quotes are not valid.
{
'name': 'Alice',
'city': 'New York'
}
{
"name": "Alice",
"city": "New York"
}
How to Debug JSON Errors
Follow this step-by-step debugging workflow when you encounter JSON parse errors:
Step 1: Read the Error Message Carefully
The error message contains valuable information:
- "Unexpected token X" โ tells you which character is problematic
- "at position Y" โ tells you exactly where in the string
Step 2: Use a JSON Validator
Paste your JSON into our JSON Formatter tool. It will:
- Highlight the exact line with the error
- Show a descriptive error message
- Auto-format valid JSON with proper indentation
Step 3: Safe Parsing with try-catch
function safeParseJSON(jsonString) {
try {
return { data: JSON.parse(jsonString), error: null };
} catch (error) {
return { data: null, error: error.message };
}
}
// Usage
const result = safeParseJSON(userInput);
if (result.error) {
console.error("JSON Parse Error:", result.error);
} else {
console.log("Parsed data:", result.data);
}
๐ง Fix JSON Errors Instantly
Paste your broken JSON into our free validator tool. Get instant error highlighting, line numbers, and auto-formatting.
Open JSON Formatter โBest Practices to Avoid JSON Errors
- Always use double quotes โ Never use single quotes in JSON
- Validate before parsing โ Use a validator during development
- Use JSON.stringify() โ Let JavaScript create JSON for you
- Handle errors gracefully โ Always wrap parse in try-catch
- Check for empty responses โ Verify API responses before parsing
- Avoid manual JSON editing โ Use tools or libraries when possible
// Instead of manually writing JSON strings...
const data = {
name: "Alice",
skills: ["JavaScript", "Python"],
active: true
};
// Let JSON.stringify create valid JSON
const jsonString = JSON.stringify(data, null, 2);
console.log(jsonString);
// Output: perfectly formatted valid JSON!
Frequently Asked Questions
What causes "Unexpected token" in JSON?
The "Unexpected token" error occurs when the JSON parser encounters a character that shouldn't be there. Common causes include: unquoted property names, single quotes instead of double quotes, comments (which JSON doesn't support), trailing commas, or undefined/NaN values. The error message tells you which character (token) was unexpected and at which position.
How do I fix "Unexpected end of JSON input"?
This error means your JSON string is incomplete. Check for: missing
closing braces }, missing closing brackets
], missing closing quotes ", or you might be
trying to parse an empty string. Use our JSON Formatter to find where
brackets are unbalanced.
Can JSON have comments?
No! Standard JSON does not support comments. If you have // or
/* */ comments in your JSON, they will cause parse errors. If you need
comments, consider using JSON5 (a JSON superset that allows comments) or
store comments as regular string properties.
Why is my API response not parsing as JSON?
Common reasons: The server might be returning HTML error pages instead of
JSON. Check if the Content-Type header is application/json. The
response might be empty. There could be a BOM (Byte Order
Mark) at the start of the response. Always log the raw response before parsing
to debug.
How to validate JSON before parsing?
The simplest approach is to use try-catch: If JSON.parse()
succeeds, it's valid JSON. For development, use our JSON Formatter tool which provides detailed validation
with line numbers and error descriptions. You can also use libraries like ajv
for schema validation.
What's the difference between JSON and JavaScript objects?
JSON is a text format for data exchange with strict rules: double quotes required, no trailing commas, no comments, no undefined/functions. JavaScript objects are more flexible: single quotes allowed, trailing commas OK, can contain functions and undefined. JSON is always a string; JS objects are actual data structures.
How to escape special characters in JSON strings?
Use backslash escaping: \" for quotes, \\ for backslash,
\n for newline, \t for tab, \r for carriage return.
The easiest way is to use JSON.stringify() which automatically escapes all
special characters correctly.
Code Formatter ยฉ 2026. Professional developer tools built with privacy and performance in mind.