Levi Gans
1SyntaxError: Unexpected end of JSON inputYou call response.json(), and instead of data you get “Unexpected end of JSON input”. The stack trace points at your parsing line, which looks fine. It is fine.
The message means the parser reached the end of the text before it found a complete JSON value. Most of the time, the text was empty. So the real question isn’t what’s wrong with your JSON. It’s why the body was empty, and that answer lives on the server or in the request, not in the line that threw.
What “Unexpected end of JSON input” means
JSON.parse("") throws it. So does JSON.parse(" "), because whitespace isn’t a value. response.json() throws it for the same reason: it reads the body as text and runs the same parse. MDN lists the exception plainly: “SyntaxError: The response body cannot be parsed as JSON.”
It also fires when JSON stops right after a colon or an opening bracket. JSON.parse('{"a":') and JSON.parse('[') both give “Unexpected end of JSON input” in Node.js 24. Cut the text anywhere else and you get a different message, like Unterminated string in JSON or Expected ',' or ']' after array element. So if you see this exact wording, think “empty” first and “cut off” second.
Two near-misses look similar and mean something else:
JSON.parse(undefined)gives"undefined" is not valid JSON. You passed a variable that was never set.Unexpected token '<', "<!doctype html>" is not valid JSONmeans the server sent HTML, usually an error page or a login redirect.
The message in each browser
The wording depends on the JavaScript engine, and in Chrome it also depends on whether you called JSON.parse() or response.json(). Here’s what each runtime printed when we parsed an empty string and called .json() on an empty 200 response:
1Chrome, Edge (V8)2 JSON.parse: SyntaxError: Unexpected end of JSON input3 response.json(): SyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input45Node.js 246 both: SyntaxError: Unexpected end of JSON input78Firefox9 both: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data1011Safari (WebKit)12 JSON.parse: SyntaxError: JSON Parse error: Unexpected EOF13 response.json(): SyntaxError: The string did not match the expected pattern.
Safari’s response.json() message is the odd one. It’s a DOMException named SyntaxError, not a plain SyntaxError, and the text doesn’t mention JSON at all. That catches people, because nobody searches for “the string did not match the expected pattern” when they mean an empty body.
If you group errors by message, these five strings are one bug. Search your error tracker for all of them.
Why the body is empty
Every one of these gave “Unexpected end of JSON input” (or the browser’s equivalent) when we called .json(), unless the table says otherwise.
| Cause | Status | Body | What you see |
|---|---|---|---|
| 204 No Content | 204 | Empty | Unexpected end |
| HEAD request | 200 | Empty | Unexpected end |
| 304 Not Modified | 304 | Empty | Unexpected end |
| Server error, no body | 5xx | Empty | Unexpected end |
no-cors request | 0 | None | Unexpected end |
| Empty string stored | None | Empty | Unexpected end |
| Connection cut mid-body | 200 | Partial | TypeError instead |
| Body read twice | Any | Used | TypeError instead |
A few of these deserve more than a row.
204, 304 and HEAD. The Fetch standard defines “a null body status” as “101, 103, 204, 205, or 304”, so those responses never carry a body. A DELETE or PUT endpoint that returns 204 is the classic case: the request worked, and the parse is what failed. MDN on HEAD says that if a response to it “has a body, the response body must be ignored.” You only see a 304 in JavaScript if you set If-None-Match or If-Modified-Since yourself. Otherwise the browser cache handles it and hands you the cached 200.
Server errors with no body. A proxy timing out, a crashed handler or a load balancer with no upstream often returns 500 or 502 with nothing in it. fetch() resolves for any HTTP status, so if your code calls .json() before checking response.ok, the parse error hides the real failure. The status code was the useful part, and you never looked at it.
no-cors. A no-cors request to another origin returns an opaque response. The Fetch standard says its “status is 0” and “body is null”. The server may have sent perfect JSON. You can’t read it, by design. Chrome’s message here is slightly different: Unexpected end of input, without “JSON”. The fix is CORS headers on the server, not mode: "no-cors".
localStorage. localStorage.getItem() returns null for a missing key, and JSON.parse(null) returns null without throwing. It’s an empty string that breaks. If some code path ever stored "", every later read throws until you clear it.
A cut connection. You’d expect a truncated download to give “Unexpected end of JSON input”. It usually doesn’t. When our test server promised 100 bytes and closed the socket early, the body read itself failed before any parsing happened: TypeError: Failed to fetch in Chrome, Content-Length header of network response exceeds response Body. in Firefox, Load failed in Safari and terminated in Node.js. A body that ends cleanly but early, from a streaming bug or a server that crashed mid-write, does reach the parser. Then the message depends on where it stopped.
Reading the body twice. A body is a stream, and you can read it once. The second .json() (or .json() after .text()) throws a TypeError: body stream already read in Chrome, Body has already been consumed. in Firefox, Body is disturbed or locked in Safari, Body has already been read in Node.js. That’s a different error, so if you see it alongside the parse error, look for a logging helper that calls .text() before your code does. response.clone() lets you read twice on purpose.
How to tell them apart in DevTools
The error object won’t tell you which case you hit. The Network panel will.
- Open DevTools, go to the Network panel and trigger the failing call.
- Select the request and look at the status.
204,304, a5xxor aHEADmethod already explains it. - Open the Response tab, which shows the response body. Empty means the server sent nothing. HTML means you got an error page.
- In the Headers tab, check
Content-TypeandContent-Length.Content-Length: 0settles it. - If the request shows
(failed)instead of a status, the connection broke. That’s a network problem, covered in our Failed to fetch guide.
To check outside the browser, right-click the request and choose Copy as cURL, then add -i to see the status and headers:
1curl -i 'https://api.example.com/items/42' -X DELETEIf curl gets a full JSON body and the browser doesn’t, compare the two requests. The usual differences are a missing cookie, a no-cors mode or a conditional header.
Parse JSON without the surprise
Read the body as text once, then decide what to do with it. You get the status, the length and the raw text in one place, and an empty body becomes a value you handle instead of an exception.
1async function readJson<T>(response: Response): Promise<T | null> {2 const text = await response.text();34 if (!response.ok) {5 throw new Error(`HTTP ${response.status} from ${response.url}: ${text.slice(0, 200)}`);6 }7 if (text.trim() === "") return null;89 const type = response.headers.get("content-type") ?? "";10 if (!type.includes("json")) {11 throw new Error(`Expected JSON from ${response.url}, got ${type || "no content type"}: ${text.slice(0, 200)}`);12 }1314 try {15 return JSON.parse(text) as T;16 } catch (error) {17 throw new Error(`Invalid JSON from ${response.url} (${text.length} chars): ${text.slice(0, 200)}`, { cause: error });18 }19}
We ran it in Node.js 24 against five local endpoints. A 204 returned null. A 500 HTML page threw HTTP 500 from .../500html: <!doctype html>.... A 200 HTML login page threw Expected JSON ..., got text/html. A body cut off mid-array threw Invalid JSON ... (13 chars): {"items":[1,2. Valid JSON came back parsed.
Why check the status before the length? Because an empty 500 is a server failure, and returning null for it would hide an outage behind an empty list. The error messages also carry the URL and the first 200 characters of the body. That’s what you need when the report lands in your error tracker, and it’s exactly what the original “Unexpected end of JSON input” left out.
Checking Content-Length alone isn’t enough. A chunked response has no Content-Length at all, and a missing header doesn’t mean an empty body. Reading the text is the only check that’s always right.
For localStorage, treat a missing or empty value as “nothing saved” and a broken one as something to clear:
1function readStored<T>(key: string, fallback: T): T {2 const raw = localStorage.getItem(key);3 if (raw === null || raw.trim() === "") return fallback;4 try {5 return JSON.parse(raw) as T;6 } catch {7 localStorage.removeItem(key);8 return fallback;9 }10}
On the server side, the fix is usually one line. Return 204 with no body and tell the client to expect it, or return 200 with {} or []. Pick one per endpoint and keep it.
Catch it in production with Interfere
Interfere is an error tracker for web apps that sorts each problem by how many users it affects, shows which customer accounts and customer tiers it hits, investigates the likely cause, and hands it to your coding agent over MCP.
Error tools were built for teams that shipped a few times a week and read every diff. Now coding agents push hundreds of pull requests to production a day, and nobody reads them all. One of them changes an endpoint to return 204, and the client that parsed its body starts throwing. Interfere does the first pass. It groups the errors, works out who each problem hurts, investigates the cause and hands the fix to an agent. It’s how we keep our sanity shipping this way.
Parse errors are a good example of why that matters. A harmless one from a browser extension can fire 40,000 times a day, while the one from your checkout endpoint hits six people. Interfere groups related occurrences into one problem and the Inbox sorts by affected users, so the quiet one gets its own row.
The Inbox also shows which customer tiers each problem reaches, using tiers you set through our API, and Companies names the accounts. Four users on Enterprise can matter more than four hundred on Free, and you set the priority to match. We think that’s a better way to decide what to fix than counting events.
Session replay starts up to seven seconds before the error. Interfere also takes OpenTelemetry traces and logs, so the server side of the failing request can be there too. The investigation connects the problem to a release and the code behind it. When it can show the cause in code, it suggests a fix, and your agent picks it up over MCP. Interfere never opens pull requests itself.
It’s built for web apps, with SDKs for Next.js, React with Vite and TanStack Start, and no mobile SDKs. See how to prioritize bugs by user impact, or set up Interfere.
The short version
“Unexpected end of JSON input” means the parser got an empty string, or JSON that stopped right after a colon or a bracket. Find out which request it was, open it in the Network panel, and look at the status and the Response tab. Then read bodies as text, check response.ok first, and treat an empty body as a case your code expects. The parse was never the problem. It’s just where the empty body finally got noticed.




