is not valid JSON

JavaScript Programming Language

Severity: Minor

What it means

Chrome, Edge and Node.js quote the start of the text: Unexpected token '<', "Firefox starts each message with JSON.parse:, such as JSON.parse: unexpected character at line 1 column 1 of the JSON data. MDN: the string has to be valid JSON.
Empty or cut-off text gives Unexpected end of JSON input, and "undefined" is not valid JSON means undefined was passed in.

Affected Models

  • Browsers (Chrome, Edge, Firefox, Safari) and Node.js

How to Fix It

  1. Starts with <? The server sent a web page, such as an error or login page, or the URL is wrong. Stack Overflow's accepted answer: log the response text and you will most likely see the HTML: console.log(await res.text());

    Another answer there: check the server's response in the Network tab of the browser's developer tools.

  2. Unexpected end of JSON input? The response was empty. Check res.ok, and the text, before parsing.

  3. "undefined" is not valid JSON? The variable passed to JSON.parse was never set. Check it first.

  4. Writing JSON by hand? MDN: no trailing commas, and property names in double quotes: {"foo": 1}, not {'foo': 1} or {foo: 1,}.