How to Fix JSON Errors Like "Unexpected token"
By Durim Hoti · Last updated
JSON is strict. One extra comma or a single quote makes the whole document invalid, and the message from JSON.parse() often points somewhere confusing. This guide translates the common messages and shows how to find and fix the exact spot.
Find and fix a JSON error step by step
- Open the JSON validator and paste the JSON, or click Open file.
- Read the result: the error message, its line and column, and the line itself with a
^under the problem. - Click Go to error to jump to that place in the editor, and fix it.
- Or click Try to repair under Repair JSON automatically, read the list of fixes, and click Use repaired JSON.
- Repeat until it says the JSON is valid; then format it with the JSON formatter if you like.
Example: two mistakes in one product record
{
"name": "Desk lamp",
"price": 25,
"tags": ["desk", "led",],
"inStock": True
}
The validator stops at the first problem: line 4, column 25, Trailing comma: remove the ',' before ']'. The second mistake, Python's True on line 5, only shows up after the first is fixed. The repair handles both at once:
![The JSON validator with the product JSON. Line 4 is highlighted in red. The result says Invalid JSON, Error on line 4, column 25: Trailing comma: remove the ',' before ']', with a caret under the comma. Below, Repair JSON automatically lists Removed trailing commas and Lowercased True/False/Null, with the buttons Use repaired JSON and Copy repaired JSON.](/guides/fix-json-errors/trailing-comma.webp)
{
"name": "Desk lamp",
"price": 25,
"tags": [
"desk",
"led"
],
"inStock": true
}Common JSON errors and what the messages mean
We ran each broken snippet through our validator and through JSON.parse() (V8, the engine in Chrome and Node.js). Firefox and Safari word their messages differently.
Trailing comma
{
"name": "Lamp",
"tags": ["desk", "led",],
}- Our validator (line 3, column 25): Trailing comma: remove the ',' before ']'.
- JSON.parse():
Unexpected token ']', ..."k", "led",], }" is not valid JSON - Automatic repair: Removed trailing commas
Single quotes
{
'name': 'Lamp'
}- Our validator (line 2, column 3): Property names must use double quotes ("), not single quotes (').
- JSON.parse():
Expected property name or '}' in JSON at position 4 (line 2 column 3) - Automatic repair: Replaced single quotes with double quotes
Unquoted key
{
name: "Lamp"
}- Our validator (line 2, column 3): Property names must be wrapped in double quotes.
- JSON.parse():
Expected property name or '}' in JSON at position 4 (line 2 column 3) - Automatic repair: Quoted unquoted property names
Comment
{
// product
"name": "Lamp"
}- Our validator (line 2, column 3): Comments are not allowed in JSON. Remove the comment.
- JSON.parse():
Expected property name or '}' in JSON at position 4 (line 2 column 3) - Automatic repair: Removed comments
Missing comma
{
"name": "Lamp"
"price": 25
}- Our validator (line 3, column 3): Missing ',' between properties.
- JSON.parse():
Expected ',' or '}' after property value in JSON at position 21 (line 3 column 3) - Automatic repair: Added missing commas
Python values
{"inStock": True, "discount": None}- Our validator (line 1, column 13): Invalid value 'True': JSON literals must be lowercase (true).
- JSON.parse():
Unexpected token 'T', ..."inStock": True, "dis"... is not valid JSON - Automatic repair: Lowercased True/False/Null; Replaced Python None with null
NaN
{"ratio": NaN}- Our validator (line 1, column 11): 'NaN' is not valid in JSON. Use null or a number instead.
- JSON.parse():
Unexpected token 'N', "{"ratio": NaN}" is not valid JSON - Automatic repair: Replaced undefined, NaN and Infinity with null
HTML instead of JSON
<!DOCTYPE html>
<html><body>404 Not Found</body></html>- Our validator (line 1, column 1): Unexpected '<': expected a value.
- JSON.parse():
Unexpected token '<', "<!DOCTYPE "... is not valid JSON - Automatic repair: not possible; the content itself must be fixed
Missing closing brace
{
"name": "Lamp",
"price": 25
- Our validator (line 4, column 1): Unexpected end of input: expected ',' or '}'. Is a closing '}' missing?
- JSON.parse():
Expected ',' or '}' after property value in JSON at position 34 (line 4 column 1) - Automatic repair: not possible; the content itself must be fixed
Common problems
Unexpected token '<' in a fetch() call
The response starts with <, so it is HTML: a 404 page, a login page or a server error. Check the status and content type before parsing:
const res = await fetch("/api/products");
const type = res.headers.get("content-type") ?? "";
if (!res.ok || !type.includes("application/json")) {
console.log(res.status, await res.text()); // see what came back
} else {
const data = await res.json();
}To see such errors while testing, the console of the online HTML editor helps; see how to run HTML and JavaScript online.
Unexpected end of JSON input
The text is empty or cut off: a closing } or ] is missing, or the response body was empty. Our validator says which bracket it expected.
The error points to the line after the mistake
With a missing comma, the parser only notices at the next property, so the position is the start of the following line. Look at the end of the line above.
The JSON looks fine but is still invalid
Look for invisible characters: a byte order mark at the start, or curly quotes (“ ”) pasted from a word processor instead of straight quotes ("). Our validator reports the byte order mark directly.
What to do next
Converting the fixed JSON to a config format? See JSON vs YAML for the values that change on the way.
FAQ
- What does "Unexpected token" mean in JSON?
- The parser reached a character that isn't allowed at that point, for example a ] right after a comma, a single quote, or the < of an HTML page. The character in quotes after "Unexpected token" is the one it stopped at.
- Why does my validator show only one error?
- A JSON parser stops at the first error, because everything after it can't be read reliably. Fix it and validate again, or use Repair JSON automatically, which fixes several common mistakes in one go.
- Are comments allowed in JSON?
- No. Standard JSON has no comments. Some tools accept them in their own config files (often called JSONC), but JSON.parse() and most APIs reject them.
- Is the repaired JSON always what I meant?
- The repair lists every change it made, such as removing trailing commas or replacing NaN with null. Read that list: replacing NaN with null, for example, changes the data, not just the syntax.
- Is my JSON uploaded?
- No. Validation and repair run in your browser.
Try it and keep reading
- Open the JSON validatorValidate JSON with exact errors and automatic repair.
- JSON vs YAML: Differences and Conversion PitfallsCompare JSON and YAML and avoid the values that silently change during conversion.
- How to Run HTML, CSS and JavaScript OnlineWrite HTML, CSS and JavaScript in the browser, see the result, and read the errors when it doesn't work.