Developer
Unexpected Token in JSON: How to Find and Fix It
Published 6 min read
On this page
- What does "unexpected token" actually mean?
- How do you find where the bad token is?
- Why does the error point to a less-than sign?
- What about "unexpected token u" or "unexpected token o"?
- Which words and values cause the error inside a file?
- What if a quote or line break inside a string is the problem?
- What does "unexpected end of JSON input" mean?
- How do you check that the fix is right?
- Unexpected token: quick answers
"Unexpected token in JSON" means the parser reached a character that cannot appear at that point in valid JSON, and stopped. The character it names is your best clue: a less-than sign (<) usually means you received an HTML page instead of JSON, u often means the text was the word undefined, and o often means an object was turned into the text [object Object]. Find the position of that character, look at what comes just before it, and fix that spot.
This guide explains how to read the message, then goes through the causes one by one with the exact result our validator gives.
What does "unexpected token" actually mean?
A JSON parser reads text one piece at a time and always knows what it is allowed to see next: a value, a comma, a colon, a closing bracket. A "token" is one of those pieces. When the next piece does not fit the grammar, the parser throws a SyntaxError and reports the character it did not expect, often with its position.
Browsers and versions word this differently. The same problem can appear as "Unexpected token", as a message saying the text "is not valid JSON", with or without a position. Do not rely on the exact wording. Rely on the character and the position.
How do you find where the bad token is?
Paste the text into our JSON formatter and press Validate. It reports the first problem as a line and column, prints that line, and puts a caret under the character. It also adds a plain-language hint when it recognises the cause.
The position is where the parser noticed the problem, which is sometimes one character after the real mistake. Always look at the character itself and at the one just before it.
Why does the error point to a less-than sign?
Valid JSON starts with {, [, a quote, a number, or one of the words true, false and null. A less-than sign at the very start almost always means the response was HTML, typically an error page, a login page or a "not found" page served instead of the data you asked for.
<!DOCTYPE html>
<html lang="en">
<head><title>404 Not Found</title></head>
Our validator reports Invalid JSON at line 1, column 1, with the detail that the less-than sign is an unexpected character. The fix is not in the JSON at all. Check the URL you requested, the status code of the response, and whether you need to be signed in. Open the address in a browser tab to see what the server actually sent.
What about "unexpected token u" or "unexpected token o"?
These usually come from code, not from a file.
- u: the text being parsed was the word
undefined. A variable was never set, or a response body was missing, and it was converted to text before parsing. Pastingundefinedinto the validator gives line 1, column 1, with the detail "Unexpected word. Expected true, false, null or a value in double quotes". - o: the text was
[object Object]. That is what JavaScript produces when an object is turned into a string by accident, so the object was probably already parsed and is being parsed a second time. The validator reports line 1, column 2, theo.
In both cases, log the value just before you parse it. The bug is in what you pass to the parser.
Which words and values cause the error inside a file?
JSON accepts only lowercase true, false and null, and has no NaN, Infinity or undefined.
{
"price": NaN
}
The validator reports line 2, column 12, the N, with the same "Unexpected word" detail. True with a capital T gives the same kind of error. Replace the value with null or a string, whichever the receiving program expects, and write booleans in lowercase.
What if a quote or line break inside a string is the problem?
A double quote inside a string must be escaped as \". Without the backslash, the string ends early and the parser trips over the rest.
{
"quote": "She said "hi""
}
The validator reports line 2, column 23, at hi. Its hint says a comma may be missing, because that is the usual reason for a word after a finished string. Here the real fix is different: write "She said \"hi\"". This is why the position matters more than the hint.
A real line break inside a string is also not allowed. For {"text": "line one followed by a new line and line two"}, the validator reports line 1, column 19 with the hint "A line break inside a string must be written as \n."
What does "unexpected end of JSON input" mean?
The parser ran out of text before the JSON was complete. Either the text was empty, or it was cut off, for example a response that stopped downloading or a copy and paste that missed the last line.
{
"name": "Ana",
"age": 31
The validator reports line 3, column 12 with the hint "The JSON ends too soon. A closing bracket, brace or quote may be missing." Add the missing }. If the text was empty, check why nothing was received before you look at the parser.
How do you check that the fix is right?
Validate again until it passes; the tool then shows the top level, such as "Object with 2 keys". Keep a copy of the broken text, then compare the broken and fixed versions to confirm that you changed only what you meant to.
Many errors that do not name an odd character come from JavaScript habits such as trailing commas, single quotes and comments; those are covered in JSON trailing comma and other common JSON errors. Once the file is valid, you can decide whether to format or minify it. For how checking and laying out JSON differ, see the difference between a JSON formatter and a validator.
Unexpected token: quick answers
What does "unexpected token < in JSON at position 0" mean?
The text starts with a less-than sign, so it is almost certainly HTML, not JSON. Check the request URL and the response status.
How do I fix "unexpected token u in JSON"?
You are parsing undefined. Make sure the variable or response body actually contains JSON before you parse it.
Why does the position point to the wrong place?
It points to where the parser noticed the problem. The real mistake is often the character just before it.
Can a validator fix the JSON for me?
It finds the first error and explains it, but you make the fix. Automatic guesses can change your data.
Is my JSON uploaded when I validate it?
No. The check runs in your browser and nothing is sent or stored.
Related tools
Related guides
- Developer
JSON Trailing Comma and Other Common JSON Errors
Fix a JSON trailing comma error and other common JSON mistakes: single quotes, comments, missing commas and unquoted keys, with the exact line and column.
- Developer
Format or Minify JSON? When to Use Each
Format vs minify JSON explained with one real example: sizes with 2 spaces, 4 spaces, tabs and minified, what changes, what stays, and when to use each.
- Developer
camelCase vs snake_case vs kebab-case: When to Use Each
camelCase vs snake_case vs kebab-case explained with one example, where each style is used in code, CSS and URLs, and how to convert names in seconds.