json-path/JsonPath · error · InvalidPathException

Expected boolean node

Error message

Expected boolean node

What it means

ValueNode's base asBooleanNode() throws InvalidPathException("Expected boolean node") when filter evaluation calls asBooleanNode() (from evaluate/getInput) on a node that is not a BooleanNode. It indicates the filter operator required a boolean operand but the evaluated node was of another type (string, number, json, etc.). JsonPath uses this default-throw pattern on the abstract ValueNode base class.

Source

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

    public NumberNode asNumberNode() {
        throw new InvalidPathException("Expected number node");
    }

    public boolean isStringNode() {
        return false;
    }

    public StringNode asStringNode() {
        throw new InvalidPathException("Expected string node");
    }

    public boolean isBooleanNode() {
        return false;
    }

    public BooleanNode asBooleanNode() {
        throw new InvalidPathException("Expected boolean node");
    }

    public boolean isJsonNode() {
        return false;
    }

    public JsonNode asJsonNode() {
        throw new InvalidPathException("Expected json node");
    }

    public boolean isPredicateNode() {
        return false;
    }

    public PredicateNode asPredicateNode() {
        throw new InvalidPathException("Expected predicate node");
    }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Use the actual JSON boolean literal in the filter: @.enabled == true, not == 'true'
  2. Guard the predicate so non-boolean values are excluded, e.g. $.items[?(@.enabled == true)] already excludes missing values; for string booleans normalize the data first
  3. Coerce string "true"/"false" values to real booleans in the document before evaluation
  4. Catch InvalidPathException around read/evaluation and handle it as a non-match

Example fix

// before
$.items[?(@.active == 'true')]  // active is a real boolean
// after
$.items[?(@.active == true)]
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = JsonPath.parse(json).read("$.item.enabled");
if (!(v instanceof Boolean)) throw new IllegalArgumentException("enabled must be a JSON boolean, got: " + v);

Type guard

static boolean isBooleanOperand(Object o) { return o instanceof Boolean; }

Try / catch

try {
    return JsonPath.parse(json).read("$.items[?(@.enabled == true)]");
} catch (InvalidPathException e) {
    return Collections.emptyList(); // non-boolean operand
}

Prevention

When it happens

Trigger: A filter predicate expecting a boolean operand — e.g. comparisons where one side is a boolean literal like $.items[?(@.enabled == true)] — evaluating to a non-boolean node, such as a field holding the string "true" instead of true, or a path resolving to a number/null.

Common situations: Documents serialized from string-typed sources where booleans arrive as "true"/"false" strings; flag fields missing in some records and resolving to undefined/null; mixing JavaScript-style truthy checks in filters that JsonPath does not coerce.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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