Ffile2fix
Sign in Get started

Fix "SyntaxError: Unexpected end of JSON input"

SyntaxError: Unexpected end of JSON input

The parser reached the end of the text before the JSON value was complete. Most often the input was completely empty, such as an empty API response or an empty string, and less often the JSON was cut off or is missing a closing bracket.

Also appears as: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data · SyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input · json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) · SyntaxError: Expected ',' or '}' after property value in JSON at position 512

Common causes

  • Calling res.json() on an empty body, such as a 204 No Content, a HEAD request or a redirect
  • fetch with mode: 'no-cors', which returns an opaque response whose body is always empty
  • JSON.parse('') on an empty string from localStorage, a file or a form field
  • The server crashed or hit a timeout midway, sending only part of the JSON
  • A missing closing } or ] in a hand-edited file
  • Reading a file while another process is still writing it

How to fix it

  1. Inspect the raw body. In DevTools > Network check the Response tab, or log await res.text() and its length. An empty string means there is nothing to parse, not invalid JSON.
  2. Handle empty responses. Check res.status === 204 or an empty text before parsing, and return null or a default value. Only call res.json() when the server actually sends a body.
  3. Remove mode: 'no-cors'. An opaque no-cors response never exposes its body to JavaScript. Fix CORS on the server with an Access-Control-Allow-Origin header instead.
  4. Guard JSON.parse on stored values. Use JSON.parse(localStorage.getItem('cart') || 'null') or check for an empty string first. Wrap the parse in try/catch for user-editable data.
  5. Fix truncated server output. Check server logs for fatal errors, memory limits or timeouts during the request. A PHP fatal error after output has started can end the response mid-JSON.
  6. Close the brackets in files. For a config or data file, a validator shows where the structure ends early. Add the missing } or ] and check that the file was saved completely.

JavaScript

async function getJson(url) {
  const res = await fetch(url);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  const text = await res.text();
  return text ? JSON.parse(text) : null; // empty body -> null
}

How to stop it happening again

  • Have APIs return a JSON body (even {}) or a 204 that the client knows not to parse
  • Read the body as text first and parse only if it is non-empty
  • Write files to a temporary name and rename them when complete
  • Log server errors that can cut responses short

Frequently asked questions

Why does Python say Expecting value: line 1 column 1 (char 0)?

That is Python's message for the same problem: the input is empty or does not start with a JSON value. Print the raw response or file content to see what was actually received.

The request works in Postman but fails in the browser. Why?

Browsers enforce CORS; if you added mode: 'no-cors' to get past a CORS error, the response body becomes empty. Configure CORS headers on the server instead.

Can I repair truncated JSON?

You can close open brackets to make it parse, but any data after the cut-off point is missing. Fetch or generate the data again when it matters.