If response.json() or JSON.parse() throws an error such as Unexpected token '<', "<!DOCTYPE "... is not valid JSON, the quoted text is a context excerpt from the string the parser received. It can reveal that the response was HTML rather than JSON, but the excerpt is not necessarily the whole body. In a measurement on Node.js v24.13.0 with V8 13.6, invalid inputs up to 20 characters were quoted whole; at 21 characters, V8 abbreviated the excerpt. That boundary is a measured behavior for that runtime, not a guarantee for every V8 release or JavaScript engine.
What does the quoted text in a JSON error mean?
The quoted fragment is text from the input that the JSON parser was asked to parse. It gives context around the parse failure; it is not a server-generated explanation of what went wrong. If the input starts with <!DOCTYPE or another HTML tag, the parser may report a leading < because that character is not a valid start for the JSON value it expected.
As an Amazon Associate I earn from qualifying purchases.
That makes the excerpt useful evidence, not a complete diagnosis. A response beginning with HTML may be an error page, login page, or application fallback. The unexpected character alone does not establish which one. Check the response metadata and body before deciding why the request returned it.
Why does V8 show 20 characters, then shorten the excerpt?
In a measurement reported by Coding Now in 2026, Node.js v24.13.0, using V8 13.6, displayed invalid inputs of 20 characters or fewer in full. At 21 characters, the message used an abbreviated excerpt. For an invalid character at the very start of a longer input, the excerpt was the first 10 characters followed by an ellipsis. This is a runtime-specific measurement, not a cross-version guarantee.
#1 Best Overall
V8’s parser implementation explains why excerpts vary by failure location. Its GetErrorMessageWithEllipses logic uses kMaxContextCharacters, set to 10 in the source reviewed, and chooses a start, surrounding, or end context based on where the unexpected token occurs. A failure farther into a string can therefore show text around that position, with an ellipsis at the beginning, the end, or both. It is not accurate to assume every error reveals the first 10 characters.
The implementation is on V8’s changing main branch, so its current behavior should not be treated as a permanent interface contract: V8 JSON parser source. The 20/21-character observation and runtime versions are reported by Coding Now’s 2026 article.
How can you tell whether the server returned HTML instead of JSON?
Use the browser’s Network panel to inspect the request that failed. Compare the response status, content type, final URL, redirect state, and body. An Accept: application/json request header expresses what the client prefers; it does not prove the server returned JSON.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problems- Status: Check whether the HTTP response was successful or an error.
fetch()generally resolves to aResponseeven for HTTP error statuses; parsing the body is a separate operation that can fail. - Content-Type: See whether the response declares JSON or a type such as
text/html. A declared type is evidence, but inspect the body if it does not match what you expected. - Final URL and redirects: Confirm the request ended at the expected endpoint. A followed authentication redirect, for example, can result in a login page being returned instead of API data.
- Body: Look at the response itself. HTML, plain-text gateway errors, an empty body, and a JSON payload point to different problems.
A Coding Now example illustrates why these checks matter: a missing route on its Next.js 16 development server returned a 404 HTML page with Content-Type: text/html; charset=utf-8, despite the request including Accept: application/json. That is an example from that setup, not a promise about how every Next.js server or missing route behaves.
Rank #3
How should you inspect the body in JavaScript without losing it?
For diagnostics, read the body as text once, capture the response metadata, and then parse that saved string if it is appropriate to do so. Reading a response body consumes its stream, so do not expect to call res.text() after a failed res.json() and still have the original body available.
const res = await fetch(url);
const contentType = res.headers.get("content-type") ?? "";
const body = await res.text();
if (!res.ok || !contentType.toLowerCase().includes("application/json")) {
console.error({
status: res.status,
contentType,
url: res.url,
redirected: res.redirected,
bodyExcerpt: body.slice(0, 300),
});
throw new Error("Expected a successful JSON response");
}
const data = JSON.parse(body);
This uses a simple substring check as an example, not a full MIME-type parser. Adapt the condition to the API’s actual response types and error contract. The excerpt is capped to limit logging; choose a limit appropriate for your environment and avoid logging sensitive response content.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What common causes should you check?
Once the body and metadata are visible, investigate the cause that fits the evidence rather than guessing from the first character alone.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallQuick Recap
- Wrong or missing route: A relative URL may resolve to a different path than intended, or the requested route may not exist.
- Authentication redirect: The request may have ended at a login page rather than the API endpoint. Check the final URL and redirect state.
- Application fallback: A single-page application may serve its HTML shell for an unrecognized path.
- Server or gateway error: The body may be an HTML error page, plain-text message, or another non-JSON response.
- Unexpected empty or malformed body: If status and content type look right, inspect the exact body and compare it with the API’s documented response contract.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




