fjordtools / Errors / Unexpected token < in JSON

Unexpected token < in JSON at position 0

The text you parsed does not start like JSON: it starts with <, so it is an HTML or XML document, not JSON. Usually the server sent an error page or a login page instead of the data you asked for. Look at the response text and the HTTP status before parsing.

Fjordtools team · Seen in Chrome and Node (V8) · Published · 1 min read

How browsers word it

Chrome and Node (V8), newerUnexpected token '<', "<html>" is not valid JSON
Chrome and Node, olderUnexpected token < in JSON at position 0
FirefoxJSON.parse: unexpected character at line 1 column 1 of the JSON data
SafariJSON Parse error: Unrecognized token '<'

Common causes

  1. 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 "<"
  2. 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 "<"
  3. 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

  1. Log the response text, not the parsed result, and read the first lines.
  2. Check the status code and the Content-Type header (it should be application/json) before calling .json().
  3. Check the URL, the route and the authentication of the request.
  4. Paste the text into the JSON formatter to see exactly where it stops being valid.

Related errors

More error explainers are coming

See all errors.