json-path/JsonPath · error · InvalidJsonException

InvalidJsonException

Error message

InvalidJsonException

What it means

JacksonJsonProvider.parse(String) wraps IOException from objectReader.readValue into InvalidJsonException. The string is not valid JSON for Jackson's reader — malformed syntax, wrong type shape (e.g. an array where a map is expected), empty input, or trailing content.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonProvider.java:73

      this(objectMapper, objectMapper.reader().forType(Object.class));
    }

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

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

    @Override
    public Object parse(byte[] json)
        throws InvalidJsonException {
        try {
            return objectReader.readValue(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 objectReader.readValue(new InputStreamReader(jsonStream, charset));
        } catch (IOException e) {

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Inspect the offending string (kept in the exception detail) and run it through a JSON validator
  2. Check the HTTP status/content-type and handle non-JSON responses before parsing
  3. Relax or fix strict-mode issues (allow comments/trailing commas) only if the producer legitimately emits them
  4. Verify the deserialization target/config; JacksonJsonProvider returns Map/List, ensure the caller expects that shape

Example fix

// before
DocumentContext ctx = JsonPath.parse(untrustedBody);
// after
if (!untrustedBody.trim().startsWith("{")) {
    throw new IllegalStateException("Expected JSON object, got: " + untrustedBody);
}
DocumentContext ctx = JsonPath.parse(untrustedBody);
Defensive patterns

Strategy: try-catch

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 isJsonText(String s) {
    if (s == null) return false;
    String t = s.trim();
    return t.startsWith("{") || t.startsWith("[");
}

Try / catch

try {
    DocumentContext ctx = JsonPath.parse(jsonString);
} catch (InvalidJsonException e) {
    log.error("JSON parse error: {} | input={}", e.getCause(), e.getMessage());
    throw new BadRequestException("Malformed JSON");
}

Prevention

When it happens

Trigger: JsonPath.parse(String) with the JacksonJsonProvider given malformed JSON, an HTML error page, JSON with comments/trailing commas in strict mode, or input whose root type conflicts with the expected mapping.

Common situations: APIs returning error pages on 401/500; hand-edited config JSON with syntax mistakes; provider differences after migrating between Gson/Jackson/Jackson3 providers; empty strings from failed file reads.

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/ee966ef09cd60927. Report an issue: GitHub.