Guides

How to Fix Invalid JSON Errors: 15 Common Mistakes & Solutions [Copy-Paste Fix]

9 min read
January 24, 2026
invalid json error, json syntax error, fix json

"Unexpected token" errors can waste hours of development time. JSON is strict—one misplaced comma or quote breaks everything.

This guide covers the 15 most common JSON errors with instant fixes you can copy-paste.

How to Diagnose JSON Errors#

Most JSON errors fall into a few categories. Here's how to read error messages:

  • "Unexpected token" — Usually a syntax character in the wrong place
  • "Unexpected end of JSON" — Missing closing bracket or brace
  • "Expected comma or ] after array element" — Missing comma

Best Tools: Use JSON Formatter to validate and see exactly where errors occur.

Error #1: Trailing Commas#

The Problem: JSON doesn't allow trailing commas (unlike JavaScript).

❌ Invalid:

{
  "name": "John",
  "age": 30,  ← Extra comma
}

✅ Fixed:

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

Why it happens: Developers copy JavaScript objects or add/remove properties without updating commas.

Error #2: Single Quotes Instead of Double Quotes#

The Problem: JSON requires double quotes (" ") for strings. Single quotes (' ') are invalid.

❌ Invalid:

{'name': 'John'}

✅ Fixed:

{"name": "John"}

Quick Fix: Search and replace all single quotes with double quotes (be careful with apostrophes in strings!).

Error #3: Unescaped Quotes in Strings#

The Problem: Quotes inside strings must be escaped with backslash.

❌ Invalid:

{"message": "He said "Hello""}

✅ Fixed:

{"message": "He said \"Hello\""}

Escape Sequences:

  • \" — Double quote
  • \\ — Backslash
  • \n — Newline
  • \t — Tab

Error #4: Missing Commas#

The Problem: Each property/array item needs a comma separator.

❌ Invalid:

{
  "name": "John"
  "age": 30
}

✅ Fixed:

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

Error #5: Comments Not Allowed#

The Problem: JSON doesn't support comments (// or /* */).

❌ Invalid:

{
  // User data
  "name": "John"
}

✅ Options:

  1. Remove comments entirely
  2. Use JSON5 or JSONC (if supported)
  3. Add a "_comment" property: "_comment": "User data"

Error #6: undefined, NaN, Infinity#

The Problem: JavaScript values undefined, NaN, and Infinity aren't valid JSON.

❌ Invalid:

{
  "value": undefined,
  "calculation": NaN
}

✅ Fixed:

{
  "value": null,
  "calculation": null
}

Use null or omit the property entirely.

JSON Debugging Checklist#

When you encounter a JSON error:

  1. Paste into JSON Formatter — sees exact line
  2. Check for trailing commas
  3. Verify all quotes are double quotes
  4. Look for unescaped special characters
  5. Count opening/closing braces and brackets
  6. Remove any comments
  7. Validate property names are in quotes

Prevention: Use a linter (ESLint, Prettier) and JSON validation in your IDE.

Frequently Asked Questions

Why do I get "Unexpected token" errors?
This usually means there is an invalid character at that position—often a missing comma, extra comma, single quote instead of double quote, or unescaped quote inside a string.
Can I use trailing commas in JSON?
No, standard JSON does not allow trailing commas. However, JSON5 and JSONC (JSON with Comments) do support them.
How do I escape quotes in JSON strings?
Use a backslash before the quote: \" inside a string becomes \". For example: {"text": "She said \"hello\""}
Can I add comments to JSON?
No, standard JSON does not support comments. Consider using JSON5 or JSONC if you need comments, or add a "_comment" property as a workaround.
What is the difference between null and undefined in JSON?
null is a valid JSON value; undefined is not. If a property is undefined in JavaScript, either use null or omit the property when converting to JSON.

Format & Validate Your JSON

Use our JSON formatter to instantly find and fix syntax errors. See exactly where your JSON is invalid.

Try JSON Formatter

Related Articles