google/gson · error · JsonSyntaxException
Did not consume the entire document.
Error message
Did not consume the entire document.
What it means
Thrown as JsonSyntaxException by JsonParser.parseReader(Reader) (JsonParser.java:112) after successfully parsing one top-level JSON value if the reader still has content — i.e. element is not JsonNull and jsonReader.peek() != JsonToken.END_DOCUMENT. It means the input contained more than a single top-level JSON document or had trailing tokens. (Trailing whitespace is fine; trailing tokens are not.)
Solutions
- For NDJSON/streaming, parse one document per line or drive a JsonReader loop with parseReader(JsonReader) which does NOT enforce single-document consumption
- Trim and inspect the raw input for trailing tokens before parsing
- Verify the source emits exactly one top-level JSON value
- If multiple documents are intentional, switch to the JsonReader overload and loop until END_DOCUMENT
Example fix
// before
JsonElement e = JsonParser.parseString(ndjsonLine); // fails if two docs on one line
// after (per-line NDJSON)
for (String line : raw.split("\n")) {
if (!line.isBlank()) JsonParser.parseString(line);
}
// after (streaming, tolerates trailing docs)
try (JsonReader r = new JsonReader(new StringReader(raw))) {
while (r.peek() != JsonToken.END_DOCUMENT) {
JsonElement e = JsonParser.parseReader(r);
}
} Defensive patterns
Strategy: validation
Validate before calling
// ensure exactly one top-level JSON value before parsing
String trimmed = raw.strip();
// simplest: parse and then re-peek via the JsonReader overload which does NOT enforce single-doc:
try (JsonReader r = new JsonReader(new StringReader(trimmed))) {
JsonElement e = JsonParser.parseReader(r);
// r.peek() == END_DOCUMENT here means no trailing tokens
} Try / catch
try {
JsonElement e = JsonParser.parseString(raw);
} catch (JsonSyntaxException ex) {
if (ex.getMessage().contains("Did not consume the entire document")) {
// trailing data: switch to per-line or JsonReader streaming parse
}
} Prevention
- For NDJSON, parse one document per line instead of the whole blob
- Use parseReader(JsonReader) when trailing/multiple documents are valid
- Inspect raw input for stray tokens after the top-level value
When it happens
Trigger: Input with multiple concatenated top-level values or trailing characters: '{ }{ }', '1 2 3', '{}' followed by stray text, two JSON objects joined, or a value followed by a stray comma. Also feeding newline-delimited JSON (NDJSON) to parseString, which expects exactly one document.
Common situations: NDJSON/log streams read as a single document; a buffer that accidentally concatenated two responses; stray BOM or paste artifacts; a trailing comma after the top-level value.
Related errors
- Failed parsing JSON source
- See
- End of input
- Expected a long but was
- Failed parsing ' ' as BigInteger; at path
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/2706ed57eb8d436a.
Report an issue: GitHub.
Appendix: source
Thrown at gson/src/main/java/com/google/gson/JsonParser.java:112
/**
* Parses the complete JSON string provided by the reader into a parse tree. An exception is
* thrown if the JSON string has multiple top-level JSON elements, or if there is trailing data.
*
* <p>The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}.
*
* @param reader JSON text
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
* @throws JsonParseException if there is an IOException or if the specified text is not valid
* JSON
* @since 2.8.6
*/
public static JsonElement parseReader(Reader reader) throws JsonIOException, JsonSyntaxException {
try {
JsonReader jsonReader = new JsonReader(reader);
JsonElement element = parseReader(jsonReader);
if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
throw new JsonSyntaxException("Did not consume the entire document.");
}
return element;
} catch (MalformedJsonException | NumberFormatException e) {
throw new JsonSyntaxException(e);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
/**
* Returns the next value from the JSON stream as a parse tree. Unlike the other {@code parse}
* methods, no exception is thrown if the JSON data has multiple top-level JSON elements, or if
* there is trailing data.
*
* <p>If the {@linkplain JsonReader#getStrictness() strictness of the reader} is {@link
* Strictness#STRICT}, that strictness will be used for parsing. Otherwise the strictness will be
* temporarily changed to {@link Strictness#LENIENT} and will be restored once this method
* returns.View on GitHub (pinned to 310ac341f2)