How to Fix Invalid JSON: Common Errors and Solutions

Invalid JSON is one of the most frustrating errors in development. This guide covers the 8 most common JSON errors, shows you exactly how to identify them, and gives you step-by-step fixes.

What Makes JSON Invalid?

JSON (JavaScript Object Notation) has a strict specification defined by RFC 8259. Any deviation from this spec makes the JSON invalid. The problem is that many developers write JSON-like structures that work in JavaScript but fail strict JSON parsing.

The 8 Most Common JSON Errors (With Fixes)

Error 1Trailing Commas

Incorrect
{
  "name": "Alice",
  "age": 30,
}
Correct
{
  "name": "Alice",
  "age": 30
}

JSON strictly forbids trailing commas. Unlike JavaScript, you cannot leave a comma after the last property.

Error 2Single Quotes Around Strings

Incorrect
{
  'name': 'Alice',
  'age': 30
}
Correct
{
  "name": "Alice",
  "age": 30
}

JSON requires double quotes for strings and object keys. Single quotes are valid in JavaScript but not in JSON.

Error 3Unquoted Object Keys

Incorrect
{
  name: "Alice",
  age: 30
}
Correct
{
  "name": "Alice",
  "age": 30
}

All object keys in JSON must be wrapped in double quotes. Unquoted keys are valid in JavaScript objects but invalid in JSON.

Error 4Comments in JSON

Incorrect
{
  // User information
  "name": "Alice",
  "age": 30
}
Correct
{
  "name": "Alice",
  "age": 30
}

Standard JSON does not support comments. If you need comments, use JSON5 or store them in a separate field.

Error 5Undefined or NaN Values

Incorrect
{
  "name": "Alice",
  "score": undefined,
  "rating": NaN
}
Correct
{
  "name": "Alice",
  "score": null,
  "rating": null
}

JSON does not support undefined or NaN. Use null instead, or omit the property entirely.

Error 6Unescaped Special Characters

Incorrect
{
  "bio": "Line one
Line two"
}
Correct
{
  "bio": "Line one\nLine two"
}

Control characters like newlines must be escaped with a backslash. Use \n for newlines, \t for tabs, etc.

Error 7BOM (Byte Order Mark)

Incorrect
{
  "name": "Alice"
}
Correct
{
  "name": "Alice"
}

Some editors add an invisible BOM character at the start of UTF-8 files. This breaks JSON parsers. Save the file without BOM.

Error 8Incorrect Escape Sequences

Incorrect
{
  "path": "C:\Users\Alice\file.json"
}
Correct
{
  "path": "C:\\Users\\Alice\\file.json"
}

Backslashes must be escaped as \\ in JSON strings. A single backslash is interpreted as an escape character.

How to Find the Exact Error Location

When JSON.parse() throws an error, the message usually includes the line and column number. But reading these messages takes practice.

SyntaxError: Unexpected token \u007D in JSON at position 42

This means the parser hit a closing brace } where it expected something else — usually a value after a trailing comma. Count characters from the start, or use an online validator that highlights the exact position.

Step-by-Step: Fix Your JSON

1

Paste your JSON into the validator

2

Read the error message and note the line/column

3

Check the 8 common errors above — does it match?

4

Apply the fix

5

Validate again until it passes

Frequently Asked Questions

What does 'Unexpected token' mean in JSON?

It means the parser found a character it did not expect at that position. Common causes include trailing commas, single quotes, or unquoted keys.

Can I use comments in JSON?

No, standard JSON does not support comments. Use JSON5 if you need comments, or add a separate '_comment' field.

Why does my valid JavaScript object fail as JSON?

JavaScript objects allow features that JSON does not: single quotes, unquoted keys, trailing commas, undefined, NaN, and comments.

How do I find the exact line of a JSON error?

Paste your JSON into a validator. The error message usually includes the line and column number where parsing failed.

What is the fastest way to fix broken JSON?

Use an online JSON validator that shows the exact error location. Fix one error at a time, then re-validate.

Fix Your JSON Now

Paste your broken JSON into our free validator. Get instant error detection and clear fix instructions.

Try JSON Formatter Online →