json-path/JsonPath · error · InvalidJsonException
InvalidJsonException
Error message
InvalidJsonException
What it means
In JakartaJsonProvider's Reader-based parse, the JSON-P JsonReader.read() throws JsonParsingException when the input is not well-formed JSON; the provider converts that into InvalidJsonException. It signals genuinely malformed JSON content reaching the parser.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java:95
throws InvalidJsonException {
return parse(new InputStreamReader(new ByteArrayInputStream(json), StandardCharsets.UTF_8));
}
@Override
public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {
try {
return parse(new InputStreamReader(jsonStream, charset));
} catch (UnsupportedEncodingException e) {
throw new JsonPathException(e);
}
}
private Object parse(Reader jsonInput) {
try (JsonReader jsonReader = defaultJsonProvider.createReader(jsonInput)) {
JsonStructure jsonStruct = jsonReader.read();
return mutableJson ? proxyAll(jsonStruct) : jsonStruct;
} catch (JsonParsingException e) {
throw new InvalidJsonException(e);
}
// not catching a JsonException as it never happens here
}
@Override
public String toJson(Object obj) {
if (obj instanceof JsonObjectBuilder) {
obj = ((JsonObjectBuilder) obj).build();
} else if (obj instanceof JsonArrayBuilder) {
obj = ((JsonArrayBuilder) obj).build();
} else if (obj instanceof List) {
obj = jsonBuilderFactory.createArrayBuilder((Collection<?>) obj).build();
}
return obj.toString();
}
@Override
public Object createArray() {
View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Validate the JSON string with a linter/parser before passing it to JsonPath.
- Check the upstream source actually returns application/json and not an error body.
- Catch InvalidJsonException and log the wrapped JsonParsingException which includes line/column of the failure.
Example fix
// before
Object doc = JsonPath.parse(responseBody);
// after
try {
Object doc = JsonPath.parse(responseBody);
} catch (InvalidJsonException e) {
LOG.error("Bad JSON at " + e.getCause().getMessage());
} Defensive patterns
Strategy: try-catch
Validate before calling
// Java
try { Json.createReader(new StringReader(payload)).read(); return true; } catch (JsonParsingException e) { return false; } Try / catch
try { Object doc = JsonPath.parse(payload); } catch (InvalidJsonException e) { JsonParsingException p = (JsonParsingException) e.getCause(); /* p.getLocation() has line/column */ } Prevention
- Validate JSON before parsing
- Confirm upstream API returns application/json
- Watch for HTML error pages and truncated bodies
- Log the wrapped JsonParsingException location on failure
When it happens
Trigger: JsonPath.parse(String/Reader) with this provider where the document has syntax errors: missing brackets, unquoted keys, trailing commas, or non-JSON content such as XML or HTML.
Common situations: Consuming an API response that returned an error page, copy-pasted JSON with smart quotes, concatenated JSON documents, or truncated files.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- e
- InvalidJsonException
- JsonPathException
- JSON-P adapter does not support getLocation()
- Cannot create JSON iterator for " + value
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/d56da8b82d0837fe.
Report an issue: GitHub.