json-path/JsonPath · error · InvalidPathException

Failed to parse operator

Error message

Failed to parse operator 

What it means

LogicalOperator.fromString() maps a string like "&&", "||" or "!" (the operatorString constants of AND/NOT/OR) to a LogicalOperator enum value. If the string matches none of them, JsonPath throws InvalidPathException with "Failed to parse operator " plus the offending text. It means the filter expression contains a boolean connector the parser does not recognize.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/filter/LogicalOperator.java:30

    LogicalOperator(String operatorString) {
        this.operatorString = operatorString;
    }

    public String getOperatorString() {
        return operatorString;
    }

    @Override
    public String toString() {
        return operatorString;
    }

    public static LogicalOperator fromString(String operatorString){
        if(AND.operatorString.equals(operatorString)) return AND;
        else if(NOT.operatorString.equals(operatorString)) return NOT;
        else if(OR.operatorString.equals(operatorString)) return OR;
        else throw new InvalidPathException("Failed to parse operator " + operatorString);
    }
}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Replace word-form connectors with the Jayway operators: use "&&" for AND, "||" for OR, "!" for NOT inside [?( ... )] filters.
  2. Print/inspect the exact operator string in the exception message and fix any escaping or encoding artifacts (HTML entities, double-unescaping).
  3. Validate the whole filter expression against the Jayway syntax docs before evaluating, e.g. by parsing it once at startup in a fail-fast test.
  4. If you need a different dialect, normalize/translate the expression to Jayway syntax before calling JsonPath.parse().

Example fix

// before
String jsonPath = "$[?(@.a == 1 and @.b == 2)]";
JsonPath.read(json, jsonPath);

// after
String jsonPath = "$[?(@.a == 1 && @.b == 2)]";
JsonPath.read(json, jsonPath);
Defensive patterns

Strategy: validation

Validate before calling

// Java: reject word-form or malformed logical connectors before evaluating
static final java.util.regex.Pattern BAD_CONNECTOR =
    java.util.regex.Pattern.compile("\\b(and|or)\\b|&&&|\\|\\|\\|");
void validateFilter(String path) {
    if (BAD_CONNECTOR.matcher(path).find()) {
        throw new IllegalArgumentException("Use && / || / ! logical operators in: " + path);
    }
}

Try / catch

try {
    return JsonPath.read(json, path);
} catch (InvalidPathException e) {
    if (e.getMessage().startsWith("Failed to parse operator")) {
        throw new IllegalArgumentException("Bad logical operator in JSONPath: " + path, e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Passing a JSONPath filter containing an unsupported logical operator spelling (e.g. "and", "&&&", or a locale-decoded/mangled "&&") to JsonPath.read/parse; evaluating a cached or hand-built filter path string that was not produced by this library's grammar.

Common situations: Hand-written filters using English words ("and"/"or") instead of "&&"/"||"; filters copied from a different JSONPath dialect (Goessner vs Jayway); query strings built dynamically and accidentally truncated or escaped incorrectly (e.g. '&&' after HTML unescaping).

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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