JSON Syntax Error: Fix 'Unexpected Token' and Invalid JSON
Short answer: "Unexpected token" means the JSON parser hit a character it did not expect, almost always because of a trailing comma, single quotes, an unquoted key, a comment or a missing comma or bracket. Paste your text into the JSON Validator and it shows the line, the column and the offending line with a caret under the problem. Fix that spot, validate again, and repeat.
Why JSON is stricter than it looks
JSON looks like a JavaScript object, and that is exactly the trap. A JavaScript object literal is relaxed: it allows unquoted keys, single quotes, comments and trailing commas. JSON allows none of them. It wants double-quoted keys and strings, and only these values: string, number, true, false, null, object and array. Something that works in your code can therefore still fail the moment it goes to an API or a config loader.
The validator parses with the same strict rules browsers and APIs use, so a pass here is a pass there.
The mistakes behind most errors
Line and column both start at 1. The messages below are what the validator shows, and the wording can differ a little between Chrome, Firefox and Safari because the position comes from your browser's own parser.
| Mistake | Example | What you see | Fix |
|---|---|---|---|
| Trailing comma | {"a":1,} |
Expected a double-quoted property name but found } |
Remove the last comma |
| Single quotes | {'a':1} |
Expected a double-quoted property name but found ' |
Use double quotes |
| Unquoted key | {a:1} |
Expected a double-quoted property name but found a |
Write {"a":1} |
| Missing comma | {"a":1 "b":2} |
Expected , or } but found " |
Add a comma between members |
| Leading zero | {"a":01} |
Expected , or } but found 1 |
Write 1, or a string such as "01" |
NaN, Infinity, undefined |
{"a":NaN} |
Unexpected N where a value was expected |
Use null or a string |
| Comment | {"a":1} // note |
Unexpected / after the end of the JSON value |
Remove the comment |
| Raw line break in a string | a real newline inside quotes | Unescaped control character in a string | Write it as \n |
Comments: JSONC is not JSON
tsconfig.json and VS Code settings files allow comments because they are really JSON with comments (JSONC). That is fine for those tools, but a strict JSON parser rejects them. If you validate such a file as plain JSON, strip the comments first. The same goes for a package.json that fails: look for comments, trailing commas and single quotes before anything else.
Duplicate keys
{"a":1,"a":2} parses, which is why parsers rarely complain, but it is almost always a mistake, because which value wins depends on the parser. The validator reports duplicate keys as a warning.
Top-level values
A lone string or number is valid JSON. The current standard (RFC 8259) allows any JSON value at the top level, not only an object or an array. Some older tools still insist on an object, so if one rejects your "hello", that is the tool, not your data.
How to fix a JSON unexpected token error with the JSON Validator
- Open the JSON Validator.
- Paste your JSON.
- If it is invalid, read the line, the column and the offending line with its caret. Fix it and validate again. An early error can hide later ones, so expect to repeat.
- If it is valid, you get the root type, the size, the number of values and keys, and how deeply it is nested.
- Reformat a valid document with the JSON Formatter, or explore a long one in the JSON Tree Viewer (to pull out specific values, see the JSONPath cheat sheet).
When the data came from an AI assistant rather than a person, mistakes like code fences and chat text are likelier. See Fix invalid JSON from ChatGPT and other LLMs, and for plain broken JSON with one safe fix, the JSON Repair tool.
Syntax is not structure
Valid JSON can still be wrong for your application: missing fields, wrong types, a number where a string belongs. To get types that match a sample, see generating TypeScript, Go and Python types from JSON. To check the shape, test the document against a schema with the JSON Schema Validator, which gives the path of every error.
Limits to know
- The error position and wording come from your browser's JSON parser, so they vary a little between browsers.
- The validator checks syntax only.
- Nothing is uploaded. Validation runs in your browser.
Frequently asked questions
What does "Unexpected token" mean in JSON? The parser found a character that is not allowed at that point, such as a trailing comma, a single quote or a comment.
Does JSON allow trailing commas or comments? No. Both are errors in strict JSON.
Why does my package.json fail validation?
Look for comments, trailing commas and single quotes.
Is a plain string or number valid JSON? Yes. RFC 8259 allows any JSON value at the top level.
Can JSON have duplicate keys? It parses, but it is usually a mistake, and the validator warns about it.
Related tools and guides
- JSON Formatter and JSON Minifier
- JSON Repair
- JSON Schema Validator (see JSON to TypeScript, Go and Python types)
- YAML to JSON: anchors, merge keys and why yes is text
- XML well-formedness errors
- JSON Diff (see Compare two JSON files)
- OpenAPI validation errors when the JSON is an API spec
- YAML syntax errors guide for the YAML equivalent