How to Fix "Unexpected token" Errors in JSON
Few errors are as familiar — or as frustrating — as SyntaxError: Unexpected token in JSON. It shows up the moment JSON.parse hits a character it didn't expect, and the message rarely tells you in plain terms what's actually wrong. The good news: almost every occurrence comes down to a short list of mistakes. This guide explains what the error means, the usual culprits, and how to fix invalid JSON in seconds.
What "Unexpected token" actually means
JSON is a strict format, and JSON.parse refuses to guess. When it reaches a character that isn't allowed at that point in the document — a stray comma, a single quote, an unquoted word — it stops and throws, reporting the unexpected token and, in most engines, the position where it gave up.
So the error isn't really about the token it names; it's about something just before it being malformed. The position number is your starting point: count to that character (or paste into a validator that shows the line and column) and the real problem is usually right there or immediately before it.
The usual culprits
The overwhelming majority of "Unexpected token" errors are one of these:
- A trailing comma after the last item in an object or array.
- Single quotes instead of the double quotes JSON requires.
- Unquoted keys — legal in JavaScript, invalid in JSON.
- Comments (// or /* */), which JSON does not allow.
- A missing or extra comma between values.
- An unescaped double quote or newline inside a string.
- JavaScript values like undefined, NaN, or Infinity.
- Python literals — True, False, or None instead of true, false, null.
Common variants of the message
The exact wording hints at the cause:
- "Unexpected token o in JSON at position 1" — you parsed something that was already an object (often calling JSON.parse twice).
- "Unexpected end of JSON input" — the document is truncated; a string or bracket is never closed.
- "Unexpected non-whitespace character after JSON" — there's extra content after a complete value, such as a second object or trailing prose.
- "Unexpected token < in JSON" — you received HTML (often an error page) where JSON was expected.
How to find and fix the bad character
Start with the position or line/column from the error and look at that spot. A validator that reports the exact line and column — and explains the problem in plain English — turns a guessing game into a one-line fix.
When the JSON came from a log file, a chat tool, or a half-finished edit, fixing it by hand is tedious. A repair tool is faster: it tolerantly parses the common mistakes above, rewrites them into valid JSON, and shows you exactly what it changed. Markdown code fences and trailing prose pasted from an AI assistant are stripped automatically, and truncated payloads get their open strings and brackets closed.
Avoiding the error in the first place
Two habits prevent most of these. First, never hand-build JSON by string concatenation — serialize a real value with JSON.stringify so quoting and escaping are correct by construction. Second, validate JSON before you ship or store it, so a stray comma is caught at your desk instead of in production.
Try it yourself
Paste broken JSON and get valid JSON back — with a list of every fix applied.