JSON

How to Fix "SyntaxError: Unexpected end of JSON input"

SyncTonight Team6 min read1 views0 likes

"Unexpected end of JSON input" is JavaScript's way of telling you that JSON.parse() started reading a JSON string and ran out of characters before finding a valid, complete structure — an opening brace with no matching closing brace, for example, or a string that just stops mid-object.

The most common real-world cause is a network response that got cut off before it fully arrived. This happens with large API responses on flaky connections, or when a server times out mid-response and closes the connection before sending the closing bytes of the JSON body.

It also happens constantly with an empty response body — the exact same underlying issue as the "position 0" error, just triggered differently depending on the JavaScript engine and exact input. An empty string technically has zero characters, so JSON.parse() reaches the end of input immediately, before it's found anything to parse.

A third common source is manually building a JSON string yourself — for example, concatenating pieces of JSON from multiple sources, or reading a JSON file from disk in chunks and parsing before the read has actually finished. If any part of that assembly is asynchronous, it's easy to accidentally call JSON.parse() on a partial string.

To debug this properly, first check the length of the string you're parsing. A suspiciously short length, or a length that changes between requests when it shouldn't, points directly at truncation rather than a formatting mistake.

If you're dealing with a fetch() response, check the response status and headers before parsing — a response with Content-Length set but a shorter actual body indicates the connection was interrupted. Wrapping the fetch in a try/catch and logging response.status alongside the raw text (via response.text() instead of response.json() while debugging) will show you exactly what arrived.

If the issue is a genuinely empty response, decide explicitly how your code should handle that case rather than letting JSON.parse() throw — many APIs legitimately return an empty body for certain successful requests (like a 204 No Content response), and treating an empty string as "no data" rather than an error is usually the correct behavior.

For quick sanity checks while debugging, paste the exact string you're trying to parse into our JSON Formatter — it will tell you immediately whether the JSON is incomplete and, if it's valid, format it so you can visually confirm the structure is what you expect.

Found this helpful?

SyncTonight's tools and guides are free and always will be. If this post saved you some debugging time, a coffee goes a long way — no pressure, just appreciated.

☕ Buy me a coffee

Keep Reading

Also available

We also build websites.

Need a landing page, a full product site, or a custom web app built? We design and develop those too — same speed and no-nonsense approach you see here. Let us know what you're building.

Landing pagesFull websitesWeb appsSaaS MVPsDashboards
Let's talk about your project