buildFix Guide

How to Fix Invalid JSON

Invalid JSON is frustrating — but every JSON syntax error has a clear pattern and a clear fix. This guide walks you through diagnosing the error, finding the line, and applying the right fix for every common case.

The 4-Step JSON Fix Process

1
content_paste

Paste into Validator

Copy your broken JSON and paste it into the JSON Validator to get the exact error message and line number.

2
search

Read the Error

The error message tells you what went wrong. "Unexpected token ," usually means trailing comma. "Unexpected end" means a missing bracket.

3
edit

Apply the Fix

Find your error type below and follow the step-by-step fix. Each section has an invalid/valid code pair.

4
check_circle

Re-validate

Paste the fixed JSON back into the validator. If there are multiple errors, fix them one at a time from the top.

error

Trailing Comma

Most Common
SyntaxError: Unexpected token }

cancel Invalid JSON

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

check_circle Fixed JSON

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

Fix Steps

  1. 1Find the last key-value pair before a closing } or ]
  2. 2Remove the comma after that last item
  3. 3Re-validate your JSON

Tip: Most text editors with JSON syntax highlighting will highlight trailing commas as errors.

error

Single Quotes Instead of Double Quotes

Very Common
SyntaxError: Unexpected token '

cancel Invalid JSON

{
  'name': 'Alice',
  'age': 30
}

check_circle Fixed JSON

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

Fix Steps

  1. 1Use Find & Replace to replace all ' with "
  2. 2Be careful not to replace apostrophes inside string values
  3. 3Validate the result

Tip: JSON requires double quotes everywhere. Single quotes are only valid in JavaScript, not JSON.

error

Unquoted Object Keys

Very Common
SyntaxError: Unexpected token n

cancel Invalid JSON

{
  name: "Alice",
  age: 30
}

check_circle Fixed JSON

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

Fix Steps

  1. 1Find all object keys (words before the : colon)
  2. 2Wrap each key in double quotes
  3. 3Validate the result

Tip: In JavaScript, object keys are optional to quote. In JSON, they are always required.

error

Comments in JSON

Common
SyntaxError: Unexpected token /

cancel Invalid JSON

{
  "name": "Alice" // the user
  // "age": 30
}

check_circle Fixed JSON

{
  "name": "Alice"
}

Fix Steps

  1. 1Remove all // line comments
  2. 2Remove all /* block comments */
  3. 3Remove commented-out keys entirely

Tip: JSON has no comment syntax by design. Use JSON5 or JSONC if you need comments during development.

error

undefined or NaN Values

Common
SyntaxError: Unexpected token u

cancel Invalid JSON

{
  "value": undefined,
  "score": NaN,
  "limit": Infinity
}

check_circle Fixed JSON

{
  "value": null,
  "score": null,
  "limit": null
}

Fix Steps

  1. 1Replace undefined with null
  2. 2Replace NaN with null or a valid number
  3. 3Replace Infinity with null or a large number

Tip: undefined, NaN, and Infinity are JavaScript values. JSON only supports null as a non-value.

error

Missing or Extra Bracket

Common
SyntaxError: Unexpected end of JSON input

cancel Invalid JSON

{
  "users": [
    {"name": "Alice"},
    {"name": "Bob"}

}

check_circle Fixed JSON

{
  "users": [
    {"name": "Alice"},
    {"name": "Bob"}
  ]
}

Fix Steps

  1. 1Count opening { and [ brackets
  2. 2Count closing } and ] brackets — they must match
  3. 3Add or remove brackets to balance them

Tip: Use a JSON formatter — it will visually indent the structure and make missing brackets obvious.

error

Unescaped Special Characters in Strings

Less Common
SyntaxError: Bad control character in string literal

cancel Invalid JSON

{
  "message": "line one
line two",
  "path": "C:UsersAlice"
}

check_circle Fixed JSON

{
  "message": "line one\nline two",
  "path": "C:\\Users\\Alice"
}

Fix Steps

  1. 1Replace actual newlines inside strings with \n
  2. 2Replace backslashes with \\
  3. 3Replace tabs with \t

Tip: Control characters and backslashes must be escaped inside JSON strings. Use JSON.stringify() to safely serialize values.

Still Stuck?

If you've applied the fix but still get a parse error, there may be multiple errors in your JSON. Parsers typically report only the first error they encounter. Fix errors one at a time from the top of the file, re-validating after each fix.

For complex or large JSON, try the JSON Formatter — it will attempt to pretty-print your JSON and may make structural problems visually obvious. The JSON Viewer shows JSON as a collapsible tree which makes missing brackets easy to spot.

FAQ – Fixing Invalid JSON

How do I fix a JSON syntax error?expand_more

Paste your JSON into a JSON validator to get the exact error message and line number. Then fix the specific problem — common fixes include removing trailing commas, replacing single quotes with double quotes, quoting object keys, and removing comments.

How do I find invalid JSON in a large file?expand_more

Use a JSON validator or formatter tool — it will pinpoint the exact line number of the first syntax error. For very large files, try splitting the JSON into smaller chunks to isolate the problem section.

Can I fix JSON automatically?expand_more

Some JSON formatters can auto-fix simple issues like trailing commas and whitespace. However, most syntax errors require manual fixes because the parser cannot guess your intent — for example, it cannot know whether a missing comma or a missing closing bracket is the real problem.

What does "Unexpected token" mean in a JSON error?expand_more

An "Unexpected token" error means the JSON parser encountered a character it didn't expect at that position. This is usually caused by a trailing comma, a single quote instead of a double quote, an unquoted key, or a comment inside the JSON.

How to Fix Invalid JSON – Step-by-Step JSON Error Fix Guide