JSON Repair Tool

“Unexpected End of JSON Input”: Empty Body, Cut-Off Payload, or Unclosed Bracket

“Unexpected end of JSON input” is what JSON.parse throws when you hand it nothing to parse. The message reads like a syntax problem. Most of the time it is a network problem, or the code called .json() on a 204. The three causes separate fast if you look at the raw text first instead of the exception.

The message means exactly what it says, which is the part that trips people up. The parser reached the end of the input while it was still waiting for a value. When the input is an empty string, the end of the input is character zero, so “end of JSON input” is a confusing way of saying there was no input. Chrome and Node both throw SyntaxError: Unexpected end of JSON input for the empty case. A payload cut off mid-document usually gets a different message, something like “Expected ',' or ']' after array element in JSON at position 33,” so the exact wording already narrows the suspect list.

“Unexpected end of JSON input” usually means the response had no body

The single most common cause: your code called .json() or JSON.parse on a response that contained nothing. A 204 No Content response is the classic. response.ok is true for a 204, so the usual if (!res.ok) throw guard sails right past it, and then res.json() explodes on an empty body. Empty error bodies do the same thing. So does reading a zero-byte file, like a config.json that another process is still writing when your code opens it.

The fix is a habit: never parse before you have looked at the text. Read the body once with response.text(), log the status and the length, and only then decide what to do with it.

const res = await fetch(url);
const text = await res.text();
console.log('status:', res.status, '| length:', text.length);

if (res.status === 204) return null;
if (!text.trim()) throw new Error('Expected JSON but received an empty response');
const data = JSON.parse(text);

That console.log line is not decoration. I have watched this error burn twenty minutes of someone's afternoon, and it always ends the same way: they print the text, see length: 0, and the mystery dissolves. The exception tells you the parse failed. The length tells you why.

When the JSON started fine and stopped early

The second family is a truncated response. The server began sending valid JSON and something stopped it: a timeout, a proxy buffer limit, a body-size cap, the server crashing mid-write. The document reads like {"name": "Alice", "items": ["apple", "banana", and then nothing. In this case the parser's position error points at the very end of the input, which is your sign. If the reported position is the last character of the text you received, suspect the transport, not your JSON.

Prove it before you fix it. Servers declare how much they intended to send in the content-length header. Compare that with what actually arrived.

const declared = res.headers.get('content-length');
const received = new TextEncoder().encode(text).length;
console.log('declared:', declared, '| received:', received);

Fewer bytes received than declared means the response was truncated in transit. Re-fetch; do not try to parse half a document. Also check the tail. The last 80 characters tell you which failure you have:

The text ends withWhat it means
{ or [A structure was opened and never closed
, or :A value was expected and never arrived
Mid-token, like tru or 12.The document was cut off mid-value

The unclosed bracket: hand-written JSON with a missing closer

The third cause is the plainest: you wrote or edited the JSON yourself and left a bracket open. An object missing its final }, an array missing its ], a string left unterminated. Here the tail check from above does the real work, because you can see the open structure with your own eyes once you look at the last characters.

This is the one case where a repair tool is the right answer. Pasting the fragment into a validator shows you the exact open structure instead of forcing you to count braces by hand. The other two causes are not syntax problems at all, and no repair tool can fix an empty body or a truncated network response. That distinction is the whole article, really: three failures, one message, and only one of them is actually broken JSON.

The thing you must not do with a truncated payload

When people learn the tail is missing its closers, the instinct is to append them. Close the brackets, parse it, move on. For a truncated network response that is data loss with extra steps. The payload may be missing properties and values in the middle, not just closers at the end. A payload you “repaired” by guessing its ending can pass every validation check while carrying half a record, and that is worse than an exception. The exception was the system telling the truth. Retry the request, or fetch the data again from a source you trust. Close brackets only on text you authored and know the intended shape of.

So the full habit, in one wrapper, is: check the status, read the text, refuse empties, verify the tail, compare the bytes, and only then parse. The error stops being mysterious the moment you stop feeding the parser blind.

Broken payload on your screen?

Paste it in and get valid JSON back, with each open structure identified for you.

Repair My JSON Free

Frequently asked questions

What causes “unexpected end of JSON input”?

Three things: an empty response body (a 204 No Content response, an empty error body, or a zero-byte file), a network response cut off mid-document by a timeout or proxy, or hand-written JSON with a missing closing bracket or unterminated string.

Why does fetch .json() throw on a successful response?

Because response.ok is true for a 204 No Content response, which has no body. The usual status check passes, then .json() throws on the empty body. Check res.status === 204, or read the body as text first and refuse empties before parsing.

How do I tell a truncated response from an empty one?

Read the body as text, then compare the content-length header against the bytes you actually received. If they differ, the response was truncated in transit. Inspecting the last 80 characters of the text also helps: an ending of { or [ means a structure was opened and never closed.

Can I fix a truncated payload by adding closing brackets?

No. A truncated response may be missing properties and values in the middle, not just closers at the end. Closing the brackets yourself hides data loss behind valid syntax. Re-fetch the data instead; only close brackets on text you authored and know the intended shape of.

Does this error mean my JSON syntax is wrong?

Usually not. The empty-body and truncated-response cases are transport problems, not syntax problems. Only the hand-written JSON case (a missing } or ] you typed yourself) is actually broken syntax, and that is the one case a repair tool can fix.