JSON Error Analyzer

Don't just see "Unexpected token" — understand exactly what's wrong and how to fix it. Get detailed explanations, visual highlighting, and auto-fix suggestions.

Try an example:
4 lines
Loading...

Click "Analyze Errors" to get detailed information about any JSON issues.

Common JSON Errors Quick Reference

Trailing comma
{"a": 1,}{"a": 1}
Single quotes
{'a': 1}{"a": 1}
Unquoted key
{a: 1}{"a": 1}
Missing comma
{"a": 1 "b": 2}{"a": 1, "b": 2}
undefined
{"a": undefined}{"a": null}
Raw newline
{"a": "line1\nline2"}{"a": "line1\\nline2"}

Why "Unexpected Token" Isn't Helpful

We've all seen it: SyntaxError: Unexpected token at position 142. But what does that actually mean? This tool goes beyond the cryptic error message to tell you:

  • Exactly where the error is (line and column)
  • What character is causing the problem
  • Why it's wrong in plain English
  • How to fix it with specific suggestions
  • Auto-fix for common issues

Common JSON Errors Explained

Trailing Commas

JavaScript allows trailing commas, but JSON doesn't:

// Wrong - trailing comma after "active"
{
  "name": "John",
  "active": true,  ← Remove this comma
}

// Correct
{
  "name": "John",
  "active": true
}

Why: JSON was designed to be a strict subset of JavaScript with unambiguous parsing rules. Trailing commas were excluded to keep the format simple.

Single Quotes Instead of Double Quotes

JSON requires double quotes for all strings:

// Wrong - single quotes
{'name': 'John'}

// Correct - double quotes
{"name": "John"}

Why: The JSON specification mandates double quotes. This prevents ambiguity and ensures consistent parsing across all platforms.

Unquoted Property Names

JavaScript lets you write {name: "John"}, but JSON doesn't:

// Wrong - unquoted key
{name: "John"}

// Correct - quoted key
{"name": "John"}

Why: Requiring quotes ensures that keys can contain any valid string, including spaces and special characters.

Missing Commas Between Elements

Easy to miss when editing JSON by hand:

// Wrong - missing comma
{
  "name": "John"  ← Missing comma here
  "age": 30
}

// Correct
{
  "name": "John",
  "age": 30
}

Invalid Values

Some JavaScript values aren't valid in JSON:

// Wrong - undefined isn't valid JSON
{"value": undefined}

// Wrong - NaN and Infinity aren't valid
{"value": NaN}
{"value": Infinity}

// Correct - use null or strings
{"value": null}
{"value": "NaN"}
{"value": "Infinity"}

Control Characters in Strings

Raw tabs, newlines, and other control characters must be escaped:

// Wrong - literal newline in string
{"text": "line 1
line 2"}

// Correct - escaped newline
{"text": "line 1\nline 2"}

Understanding Error Positions

When JSON parsers report an error "at position 142", they're counting from the start of the string. This tool converts that to line and column numbers, which are much easier to find in your editor.

Important: The error position is usually where the parserdetected the problem, not necessarily where the actual mistake is. For example, a missing comma might not be detected until the parser hits the next property name.

Tips for Debugging JSON

  1. Start with valid JSON — Use the JSON Repair tool to auto-fix common issues before debugging manually.
  2. Check the line above — Often the real error is on the linebefore where it's reported (like a missing comma).
  3. Look for copy-paste issues — JSON from JavaScript code often has single quotes or trailing commas that need fixing.
  4. Validate incrementally — If you have a large JSON file, try validating smaller sections to isolate the problem.
  5. Use syntax highlighting — A good editor will show mismatched brackets and unclosed strings visually.

Error Messages by Browser

Different JavaScript engines report JSON errors differently:

BrowserExample Error Message
Chrome/NodeUnexpected token , at position 42
FirefoxJSON.parse: expected property name at line 3 column 5
SafariJSON Parse error: Expected '}'

This tool normalizes these messages into a consistent, helpful format.

Programmatic Error Handling

// JavaScript - wrapping JSON.parse for better errors
function parseJSONWithDetails(text) {
  try {
    return { success: true, data: JSON.parse(text) };
  } catch (e) {
    const posMatch = e.message.match(/position (\d+)/);
    if (posMatch) {
      const pos = parseInt(posMatch[1], 10);
      const lines = text.substring(0, pos).split('\n');
      return {
        success: false,
        error: e.message,
        line: lines.length,
        column: lines[lines.length - 1].length + 1,
        context: text.substring(pos - 20, pos + 20)
      };
    }
    return { success: false, error: e.message };
  }
}

Related Tools

Frequently Asked Questions

Why does the error say line 1 when my JSON is 100 lines?

This usually happens with minified JSON (no newlines). The entire content is technically on "line 1". The column number and position will help you find the actual location.

The error position seems wrong. Why?

JSON parsers report where they detected an error, not where the mistake actually is. For example, if you forget a comma, the parser won't know until it sees the next unexpected token.

Can this fix all JSON errors?

Auto-fix works for common structural issues (trailing commas, single quotes, unquoted keys). For more complex problems, use the JSON Repair tool or fix manually using the suggestions provided.