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

  1. Print/inspect the offending string (it is included as the exception's detail) and validate with a JSON parser first
  2. Check response status/content-type before parsing; route non-JSON bodies to error handling
  3. Remove BOM, trailing commas, or comments, or enable Jackson's allowed-non-numeric-numbers/allow-comments features if appropriate
  4. 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

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


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