Unexpected end of JSON input
The document ended while the parser was still waiting for something — a closing brace, a closing quote, or in fact any content at all. Unlike most parse errors this one carries no useful position, because the problem is the absence of characters rather than a wrong one.
How the message appears
The same fault, worded by different engines. V8 (Chrome, Node, Edge) changed its format around Chrome 109, so older and newer runtimes disagree about the same document.
What causes it
The string is empty
JSON.parse("") throws this exact error. An empty string is not valid JSON — null is. This is by far the most common source, usually from a response with no body.
The response had no body to parse
A 204 No Content reply, or a fetch whose body was already consumed. A Response body is a stream and can only be read once, so calling res.text() for logging and then res.json() leaves the second call nothing to read.
Truncated output
A file cut short by a full disk, a killed process, a stream written without being flushed or closed, or a log line clipped at a length limit. Open the end of the file: if the final brace is missing, the writer never finished.
An unclosed bracket, brace or string
Structurally the same thing — the parser is still inside a value when input runs out. An unterminated string is the sneakiest, because everything after the opening quote is swallowed as string content.
The fix
Close every structure that was opened. Here both the inner object and the outer object are missing their closing brace.
{
"user": {
"name": "Ada",
"id": 7
{
"user": {
"name": "Ada",
"id": 7
}
}The example travels in the URL fragment, which browsers never send to a server.
Worth knowing
- Guard the empty case rather than catching the throw:
const data = text ? JSON.parse(text) : null. - Check
response.okand thecontent-typeheader before parsing. A failed request often returns HTML or nothing at all, and neither is JSON.