Guides / JSON errors
“Unexpected end of JSON input”
How it appears per engine
- Uncaught SyntaxError: Unexpected end of JSON input (Chrome / Node.js)
- SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data (Firefox)
- SyntaxError: JSON Parse error: Unexpected EOF (Safari)
What this error means
The parser reached the end of the text while the JSON value was still incomplete. Two very different situations produce it: the input was empty (""), or the input was cut off mid-document — an unclosed brace, bracket, or string at the very end.
Common causes
1. You parsed an empty string
The most frequent case. A 204 No Content response, an empty request body, or a missing localStorage key that was stored as "" all hand JSON.parse an empty string.
Fails
JSON.parse(''); // throws immediately
JSON.parse(localStorage.getItem('settings') ?? ''); // same problemWorks
const raw = localStorage.getItem('settings');
const settings = raw ? JSON.parse(raw) : defaultSettings;2. The payload was truncated in transit
A network timeout, a killed process writing a file, or reading a file while it is still being written leaves you with the first half of a JSON document. The parser runs off the end and throws.
3. A response body was read before it finished streaming
Assembling a body from stream chunks and parsing before the stream closes parses a prefix of the document. Always await the full body (res.json() / res.text() already do this).
How to fix it
- Guard the empty case: if (!raw) return fallback.
- Wrap JSON.parse in try/catch and log the raw string length — 0 means empty input, > 0 means truncation.
- For files, verify the writer finished (write to a temp file, then rename).
- Paste the payload into a validator to see exactly where it stops being valid.