JSON validator

Unexpected end of JSON input

SyntaxError: Unexpected end of JSON input means the parser reached the end of the text before the JSON was complete. Most often the text was empty, which happens when JSON.parse("") or response.json() runs on an empty body. When it comes from response.json(), Chrome prints Failed to execute 'json' on 'Response': Unexpected end of JSON input, and Firefox prints JSON.parse: unexpected end of data at line 1 column 1 of the JSON data.

The validator below has a cut-off JSON document loaded, so you can see the error. Text that is cut off elsewhere can show other messages instead, such as Unterminated string in JSON or Expected ',' or '}' after property value, but the fix is the same: get the complete text.

JSON validator
InputSyntax errorJSON

Press Command or Control plus Enter to run. Tab inserts indentation; press Escape, then Tab, to leave the editor.

Result
Your result will appear here.
Ready4 lines · 51 chars

Common causes

  • The response has no body: a 204 No Content, a HEAD request, or an error response with nothing in it.
  • The server crashed or the connection closed part way through the response.
  • JSON.parse got an empty string, for example from an empty file, an empty environment variable or a form field.
  • A JSON file was truncated while it was still being written.
  • Brackets or braces aren't closed, so the document stops before it's complete.

Handle empty responses in fetch

async function readJson(response) {
  const text = await response.text();
  return text ? JSON.parse(text) : null; // empty body, such as a 204
}

const response = await fetch("/api/items/7", { method: "DELETE" });
const data = await readJson(response); // null instead of a SyntaxError

Parse stored values safely

localStorage.getItem returns null for a missing key, and JSON.parse(null) returns null, so that case is fine. An empty string, or a value written by older code, is not:

function readStored(key, fallback) {
  try {
    const raw = localStorage.getItem(key);
    return raw ? JSON.parse(raw) : fallback;
  } catch {
    return fallback; // corrupted or not JSON
  }
}

Frequently asked questions

Which line is the error on?

This message has no position because the parser only notices the problem when it runs out of input. Paste the JSON into the validator and look for the first unclosed {, [ or string.

Can I parse a partial JSON response?

Not with JSON.parse. If the server streams data, use a format meant for it, such as newline-delimited JSON, where each line is a complete document.

Why does response.json() fail but the network tab shows data?

Check that you don't read the body twice. A body can only be read once, so call response.text() once and parse that, instead of calling both text() and json().

More json validator guides

Open the full json validator →

From the blog