json-path/JsonPath · error · InvalidJsonException

InvalidJsonException

Error message

InvalidJsonException

What it means

Jackson3JsonProvider.parse delegates to Jackson's ObjectMapper/ObjectReader readValue. When the input string/bytes are not valid JSON (syntax error, unexpected token, truncated input), Jackson throws a JacksonException, which the provider wraps in InvalidJsonException carrying the offending input as the message/cause.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/Jackson3JsonProvider.java:75

    }

    /**
     * Initialize the JacksonProvider with a custom ObjectMapper and ObjectReader.
     *
     * @param objectMapper the ObjectMapper to use
     * @param objectReader the ObjectReader to use
     */
    public Jackson3JsonProvider(ObjectMapper objectMapper, ObjectReader objectReader) {
        this.objectMapper = objectMapper;
        this.objectReader = objectReader;
    }

    @Override
    public Object parse(String json) throws InvalidJsonException {
        try {
            return objectReader.readValue(json);
        } catch (JacksonException e) {
            throw new InvalidJsonException(e, json);
        }
    }

    @Override
    public Object parse(byte[] json) throws InvalidJsonException {
        try {
            return objectReader.readValue(json);
        } catch (JacksonException e) {
            throw new InvalidJsonException(e, new String(json, StandardCharsets.UTF_8));
        }
    }

    @Override
    public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {
        try {
            return objectReader.readValue(new InputStreamReader(jsonStream, charset));
        } catch (IOException e) {
            throw new InvalidJsonException(e);

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Validate the payload is well-formed JSON before parsing (or catch InvalidJsonException and inspect getCause())
  2. Log the raw input when parsing fails to spot HTML/truncated responses
  3. Check HTTP status/content-type before parsing response bodies
  4. Fix the source producing invalid JSON (serializer config, encoding, truncation)

Example fix

// before
DocumentContext ctx = JsonPath.parse(responseBody); // InvalidJsonException on HTML error pages
// after
if (response.getStatus() == 200 && response.getContentType().contains("application/json")) {
    DocumentContext ctx = JsonPath.parse(responseBody);
} else {
    throw new ApiException("non-JSON response: " + response.getStatus());
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (body == null || body.trim().isEmpty()) throw new IllegalArgumentException("empty JSON input");
// optional strict pre-check with Jackson:
objectReader.readTree(body); // throws on malformed JSON before JsonPath sees it

Try / catch

try {
    return JsonPath.parse(body);
} catch (InvalidJsonException e) {
    log.error("invalid JSON: {}", body, e.getCause());
    throw new IllegalArgumentException("payload is not valid JSON", e);
}

Prevention

When it happens

Trigger: Calling JsonPath.parse(String) or parse(byte[]) with malformed JSON — trailing commas, unquoted keys, single quotes, truncated responses, HTML error pages instead of JSON, empty or whitespace-only input.

Common situations: Consuming HTTP APIs that return HTML error pages with 200/500 status; logging/truncated payloads persisted and later parsed; wrong charset/encoding corrupting bytes; hand-edited JSON fixtures.

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


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/56794287c3cb8b5e. Report an issue: GitHub.