How browsers word it
| Chrome and Node (V8), newer | Unexpected token '<', "<html>" is not valid JSON |
|---|---|
| Chrome and Node, older | Unexpected token < in JSON at position 0 |
| Firefox | JSON.parse: unexpected character at line 1 column 1 of the JSON data |
| Safari | JSON Parse error: Unrecognized token '<' |
Common causes
An error page came back
A wrong URL, an expired session or a crashed server often answers with an HTML page and a 4xx or 5xx status. The code then tries to parse that page as JSON.
Input
<!DOCTYPE html><html><head><title>404 Not Found</title></head><body>Page not found</body></html>
JSON Formatter says
Unexpected "<"
The API answered in XML
Some services return XML unless the request asks for JSON with an Accept header.
Input
<?xml version="1.0"?><a>1</a>
JSON Formatter says
Unexpected "<"
A dev server returned index.html
Single-page app servers return index.html for any path they do not know, so a mistyped API route gets your app's HTML with a 200 status.
Input
<!doctype html><html lang="en"><head>
JSON Formatter says
Unexpected "<"
How to fix it
- Log the response text, not the parsed result, and read the first lines.
- Check the status code and the Content-Type header (it should be application/json) before calling .json().
- Check the URL, the route and the authentication of the request.
- Paste the text into the JSON formatter to see exactly where it stops being valid.