JSON Parse Error: How to Fix All Common Errors (Complete Guide)

Udit Sharma Feb 6, 2026 15 Min Read 2.5K+ Monthly Searches
๐Ÿ”ง Fix JSON errors? Try JSON Formatter Free
No sign-up needed ยท 100% in-browser ยท Instant results
Open JSON Formatter โ†’
๐Ÿ”ง 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.

โŒ This Will Throw a Parse 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:

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

โŒ Wrong - Unquoted Keys
{
    name: "Alice",
    age: 25
}
โœ… Correct - Double-Quoted Keys
{
    "name": "Alice",
    "age": 25
}

2. Comments in JSON

โŒ Wrong - JSON Doesn't Support Comments
{
    "name": "Alice",
    // This is a comment
    "age": 25
}
โœ… Correct - Remove All Comments
{
    "name": "Alice",
    "age": 25
}

3. Undefined or NaN Values

โŒ Wrong - undefined and NaN are not valid JSON
{
    "value": undefined,
    "number": NaN
}
โœ… Correct - Use null for missing values
{
    "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

โŒ Wrong - Missing Closing Brace
{
    "name": "Alice",
    "skills": ["JavaScript", "Python"]
// Missing } here
โœ… Correct - All Brackets Closed
{
    "name": "Alice",
    "skills": ["JavaScript", "Python"]
}

2. Parsing Empty String

โŒ Wrong - Empty String
// This will throw "Unexpected end of JSON input"
const data = JSON.parse("");
โœ… Correct - Check Before Parsing
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

โŒ Wrong - Two Root Objects
{"name": "Alice"} {"name": "Bob"}
โœ… Correct - Use Array for Multiple Objects
[
    {"name": "Alice"},
    {"name": "Bob"}
]

Error #4: Trailing Comma

Unlike JavaScript, JSON doesn't allow trailing commas after the last element in arrays or objects.

โŒ Wrong - Trailing Comma
{
    "name": "Alice",
    "age": 25,   // This comma breaks JSON!
}
โœ… Correct - No Trailing Comma
{
    "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.

โŒ Wrong - Single Quotes
{
    'name': 'Alice',
    'city': 'New York'
}
โœ… Correct - Double Quotes Only
{
    "name": "Alice",
    "city": "New York"
}

How to Debug JSON Errors

Follow this step-by-step debugging workflow when you encounter JSON parse errors:

1. Read Error Message 2. Find Position Line Number 3. Use Validator JSON Formatter 4. Fix & Test Validate Again
JSON Error Debugging Workflow: Read โ†’ Locate โ†’ Validate โ†’ Fix

Step 1: Read the Error Message Carefully

The error message contains valuable information:

Step 2: Use a JSON Validator

Paste your JSON into our JSON Formatter tool. It will:

Step 3: Safe Parsing with try-catch

โœ… Always Use try-catch for JSON Parsing
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

  1. Always use double quotes โ€” Never use single quotes in JSON
  2. Validate before parsing โ€” Use a validator during development
  3. Use JSON.stringify() โ€” Let JavaScript create JSON for you
  4. Handle errors gracefully โ€” Always wrap parse in try-catch
  5. Check for empty responses โ€” Verify API responses before parsing
  6. Avoid manual JSON editing โ€” Use tools or libraries when possible
โœ… Let JavaScript Create Valid JSON
// 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.

JSON Error? Fix it instantly. Fix JSON Now โšก