json-path/JsonPath · error · InvalidJsonException
InvalidJsonException
Error message
InvalidJsonException
What it means
JacksonJsonNodeJsonProvider.parse(String) wraps IOException from objectMapper.readTree into InvalidJsonException. The input string could not be parsed into a Jackson JsonNode tree — it is not valid JSON, empty, or contains a BOM/trailing garbage.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java:54
public JacksonJsonNodeJsonProvider() {
this(defaultObjectMapper);
}
/**
* Initialize the JacksonJsonNodeJsonProvider with a custom ObjectMapper and ObjectReader.
*
* @param objectMapper the ObjectMapper to use
*/
public JacksonJsonNodeJsonProvider(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public Object parse(String json) throws InvalidJsonException {
try {
return objectMapper.readTree(json);
} catch (IOException e) {
throw new InvalidJsonException(e, json);
}
}
@Override
public Object parse(byte[] json)
throws InvalidJsonException {
try {
return objectMapper.readTree(json);
} catch (IOException e) {
throw new InvalidJsonException(e, new String(json, StandardCharsets.UTF_8));
}
}
@Override
public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {
try {
return objectMapper.readTree(new InputStreamReader(jsonStream, charset));
} catch (IOException e) {View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Print/inspect the offending string (it is included as the exception's detail) and validate with a JSON parser first
- Check response status/content-type before parsing; route non-JSON bodies to error handling
- Remove BOM, trailing commas, or comments, or enable Jackson's allowed-non-numeric-numbers/allow-comments features if appropriate
- Verify Configuration is intentionally using JacksonJsonNodeJsonProvider (it returns JsonNode, not Map/List) and not a misconfiguration
Example fix
// before DocumentContext ctx = JsonPath.parse(rawBody); // after if (rawBody == null || rawBody.isBlank()) throw new EmptyBodyException(); DocumentContext ctx = JsonPath.parse(rawBody);
Defensive patterns
Strategy: validation
Validate before calling
static boolean isValidJson(String s) {
if (s == null || s.isBlank()) return false;
try { new ObjectMapper().readTree(s); return true; } catch (IOException e) { return false; }
} Type guard
static boolean isJsonString(String s) {
if (s == null) return false;
String t = s.trim();
return (t.startsWith("{") && t.endsWith("}")) || (t.startsWith("[") && t.endsWith("]"));
} Try / catch
try {
DocumentContext ctx = JsonPath.parse(jsonString);
} catch (InvalidJsonException e) {
log.error("Invalid JSON input: {}", e.getMessage());
throw new BadRequestException("Input is not valid JSON");
} Prevention
- Check response status/content-type before treating a body as JSON
- Trim and BOM-strip strings from files and editors
- Keep a single shared Configuration so the intended provider is always used
- Validate sample payloads with a JSON linter in CI
When it happens
Trigger: JsonPath.parse(String) configured with the JacksonJsonNodeJsonProvider when the string is malformed JSON, empty/whitespace-only, an HTML error page, or a JSON5/comments document that Jackson's strict mode rejects.
Common situations: Downstream APIs returning HTML on auth expiry; copy-pasted JSON with trailing commas or comments; reading response bodies of failed requests without checking status; empty string from an empty file.
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
- InvalidJsonException
- Failed to parse SliceOperation:
- Not a JSON Node
- length operation can not applied to null
- InvalidJsonException
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/467586f21b34ecf7.
Report an issue: GitHub.