How to Fix Invalid JSON Syntax Errors (7 Common Causes + Quick Fixes)

You paste an API response into your code, run JSON.parse(), and the whole thing blows up with "Unexpected token". Sound familiar? JSON is famously strict — one misplaced comma or a single quote where a double quote should be, and the entire document is invalid. The good news: nearly every invalid-JSON error comes from the same handful of mistakes, and each one is easy to fix once you know what to look for.

This guide walks through the 7 most common invalid JSON syntax errors with examples, shows how to find the exact broken character in seconds, and how to validate your JSON online for free.

Why Is My JSON Invalid? The 7 Most Common Causes

1. Trailing commas

The single most common cause of "Unexpected token } in JSON". A comma after the last item is fine in JavaScript — in JSON it kills the whole document.

✗ Invalid:

{
  "name": "Usman",
  "city": "Karachi",
}

✓ Fixed — delete the comma after "Karachi":

{
  "name": "Usman",
  "city": "Karachi"
}

2. Single quotes instead of double quotes

JSON requires double quotes for every key and every string value. Single quotes give "Unexpected token ' in JSON".

✗ Invalid: { 'name': 'Usman' }
✓ Fixed: { "name": "Usman" }

3. Unquoted keys

JavaScript object literals can skip quotes on keys. JSON never can.

✗ Invalid: { name: "Usman" }
✓ Fixed: { "name": "Usman" }

4. Comments

JSON does not allow // or /* */ comments. Config files copied from documentation often carry comments with them — remove every one before sending the JSON anywhere.

✗ Invalid: { "name": "Usman" // the user }
✓ Fixed: { "name": "Usman" }

5. Missing commas between items

Every pair of elements needs a comma except the last one. When an error points at a line that looks perfectly fine, check the line above it for a missing comma.

✗ Invalid: { "first": "Muhammad" "last": "Usman" }
✓ Fixed: { "first": "Muhammad", "last": "Usman" }

6. Unescaped characters inside strings

Literal newlines, tabs, or unescaped double quotes inside a string are invalid. Escape them with a backslash.

✗ Invalid: { "path": "C:\new\file" }
✓ Fixed: { "path": "C:\\new\\file" }

7. NaN, Infinity, and undefined

These are JavaScript values, not JSON values. Use null or a real number instead.

✗ Invalid: { "score": NaN }
✓ Fixed: { "score": null }

How to Find the Exact Error in Seconds

Staring at a 500-line response won't find the broken character — let a tool point at it:

  1. Paste it into a JSON validator. Our free JSON Formatter & Validator flags the exact line and character where parsing fails, and pretty-prints the document once it's valid.
  2. Use the browser console. Paste your JSON into JSON.parse('...') — the error message tells you the exact character position where it broke.
  3. Try Python. Run python3 -m json.tool on the file: it pretty-prints valid JSON and points at the error for invalid JSON.

Fix and Format Your JSON Online (Free)

Once you've spotted the mistake, the fastest fix workflow is:

  1. Paste the broken JSON into our JSON Formatter & Validator.
  2. Read the error location, fix the character, and re-validate until it passes.
  3. Use the Format (pretty-print) mode to get clean, readable JSON, or the Minify mode to shrink it for production.

The tool runs entirely in your browser — your data is never uploaded, so it's safe even for API keys and internal payloads.

How to Prevent JSON Errors in the First Place

  • Turn on JSON language mode in VS Code or your editor — it highlights bad syntax as you type.
  • Never hand-write large JSON. Build it with JSON.stringify() (or your language's encoder) instead of string concatenation.
  • Lint config files before deploying — one trailing comma in a config can take down an app.
  • Minify only at the end. Keep a formatted copy for humans and minify for production with the tool above.

Frequently Asked Questions

Why does JSON not allow trailing commas or comments?

JSON was designed as a minimal, unambiguous data format that every language can parse the same way. Features like trailing commas and comments add parsing complexity, so the spec left them out. (JSON5 exists as a superset for config files, but APIs expect strict JSON.)

How do I validate JSON without installing anything?

Paste it into a free online validator like our JSON Formatter & Validator — it checks the syntax and points to the exact error, right in your browser.

What does "Unexpected token" mean in JSON?

It means the parser hit a character it didn't expect at that position — almost always one of the 7 mistakes above: a trailing comma, single quotes, unquoted keys, or a missing comma.

Can I convert a JavaScript object to valid JSON?

Yes — pass it through JSON.stringify(). It quotes keys, uses double quotes, and drops undefined values automatically. For a visual check, paste the output into the formatter linked above.

Is it safe to paste my JSON into an online formatter?

Only if the tool runs client-side. Our formatter processes everything in your browser, so nothing is uploaded or stored. Avoid tools that send your data to a server when it contains secrets.

Still staring at a red error? Paste your JSON into our free JSON Formatter & Validator, fix the flagged character, and pretty-print it in seconds. For more handy utilities, browse all our free online tools for bloggers.

Featured on LaunchNest