Fix unexpected end of JSON input
What the 'unexpected end of JSON input' error means, how to tell an empty response from a truncated one, and when a JSON formatter helps or misleads.
The error "Unexpected end of JSON input" means a JSON parser reached the end of its input while it still expected more. Either nothing was passed in, or the text stopped before every string, array and object was closed. The wording comes from the V8 engine used by Chromium-based browsers and Node.js. Other environments report the same condition differently. Firefox refers to an unexpected end of data, and Python's json module reports "Expecting value" at character 0 for an empty string. Messages also change between engine versions, so treat the wording as a hint, not a diagnosis.
Quick answer: is a JSON formatter or validator the right tool here?
A validator is useful for this error. It confirms whether the text is complete and shows where parsing stopped. If it stops at the very last character, the input was cut short or was empty. A formatter (pretty-printer or minifier) is safe for valid JSON because it changes only whitespace between tokens. It cannot fix this error, though, because the missing content is not in the input. "Repair" features that add closing brackets or quotes are a guess. They can turn broken data into valid JSON that is wrong, for example an array that silently lacks its last records.
Keep the raw, unmodified text when you are debugging, filing a bug or comparing against a server log. Formatted output is easier to read but hides the exact byte length and line structure. The exact length is often what shows the truncation.
What a JSON tool actually changes
- Pretty-printing: adds indentation and line breaks between tokens. Values, key order and string contents stay the same.
- Minifying: removes that insignificant whitespace. Whitespace inside strings is kept.
- Validation: changes nothing. It parses the text and reports the first position where parsing failed.
- Auto-repair: adds or removes characters to make the text parse. It changes the data, and it should not be used on content you need to trust.
Text transformations from other workflows behave very differently. Reversing characters or words, substituting stylized Unicode "font" characters, or mirroring glyphs all change the stored characters. Any of them will make JSON invalid. Reversal, for example, moves the closing brace to the front. CSS presentation such as a right-to-left direction or a visual flip changes only how the text looks on screen. The underlying characters, and normally the copied text, stay the same. Bidirectional display works the same way: right-to-left text inside a string value can appear reordered on screen while its stored order is unchanged. A payload can therefore look scrambled or cut off without actually being cut off. Check its length or the last few characters instead of trusting the rendering.
Common causes and how to tell them apart
JSON.parse(""); // V8: SyntaxError: Unexpected end of JSON input
JSON.parse('{"id": 42'); // input ends before the closing brace
| Cause | How to check | Correction |
| Empty response body (for example HTTP 204 No Content, a HEAD request, or an error handler that returns nothing) | Log the status code and read the body as text before parsing; an empty string confirms it | Handle the empty case explicitly instead of always parsing, or have the endpoint return valid JSON such as {} or null |
| Error page or non-JSON body with no content | Inspect the status code and Content-Type header in the browser's network panel or with a command-line HTTP client | Check that the response is OK before parsing, and surface the status and body text in the error |
| Truncated payload (connection dropped, proxy or size limit, a stream parsed before it finished) | Compare the received length with the expected length, and look at the final characters | Parse only after the full body has arrived, and investigate limits or timeouts on the server or proxy |
| Incomplete copy and paste (log viewer, terminal or chat message cut off long output) | Check whether the copied text ends with the expected closing bracket; copy again from the original file or raw response | Export or save the full output to a file instead of copying from a display that may truncate |
| Empty stored value (an empty string saved to a file, cache or browser storage) | Print the value and its length before parsing | Avoid writing empty strings where JSON is expected, and guard reads against empty values |
Some problems look similar but cause a different error. Curly "smart" quotes added by word processors or chat apps, a stray comma, or an HTML page returned instead of JSON usually produce an "unexpected token" or "not valid JSON" message, not an end-of-input message. If the error names a token or a position in the middle of the text, look for one of those problems instead.
A defensive pattern for fetch responses
In browser or Node.js code, the error often comes from calling response.json() on a body that turns out to be empty. Reading the body as text first lets you inspect it, handle the empty case and include useful context in the error message:
const response = await fetch(url);
const body = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${body.slice(0, 200)}`);
}
if (body.trim() === "") {
return null; // decide what "no content" means for this endpoint
}
return JSON.parse(body);
This pattern only shows and handles the symptom. If the body is truncated, not just empty, the fix belongs where the data is produced or sent.
Will the JSON stay intact after copying or sharing?
Plain JSON survives copying between code editors and plain-text fields. Rich-text destinations may change it. Word processors, email composers and some chat apps can replace straight quotes with curly ones, add non-breaking spaces or cut long messages. Behavior differs by app and by setting, and this article includes no tests of specific destinations. To check, paste the text back into a validator after sharing, or attach the JSON as a file or inside a code block where the destination supports one.
Indented JSON is generally easier to scan and to move through line by line, including with a screen reader. How punctuation such as braces and quotes is announced depends on the screen reader and its punctuation settings, so no single format can be assumed to read well everywhere. Search and editing tools work on the stored characters, so formatted JSON stays searchable. Text that has been converted to stylized Unicode or reversed does not.
Before pasting a payload into an online validator, remove tokens, passwords and personal data. A tool's appearance or marketing tells you nothing about whether it keeps what you submit. Rely on its stated policy, or use a validator that runs locally, such as your editor or a command-line tool.
Known gaps
This guide is based on general parser behavior, not on measured tests. Exact error messages differ across browsers, runtimes and versions. It does not cover specific APIs, frameworks or hosting setups that may truncate responses. If the checks above do not find the cause, capture the raw response, including status, headers and full body, from the network layer and compare it with what the server logged as sent.