json-path/JsonPath · error · InvalidPathException

Expected class node

Error message

Expected class node

What it means

ValueNode's base asClassNode() throws InvalidPathException("Expected class node") when evaluate calls it on a node that is not a ClassNode. ClassNode represents a Java Class operand used by the type-checking operator ('instanceof'-style filters like @ instanceof java.lang.String in newer JsonPath). The base class throws by default; this error means a type-check operand was not a ClassNode.

Source

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

    public NullNode asNullNode() {
        throw new InvalidPathException("Expected null node");
    }

    public UndefinedNode asUndefinedNode() {
        throw new InvalidPathException("Expected undefined node");
    }

    public boolean isUndefinedNode() {
        return false;
    }

    public boolean isClassNode() {
        return false;
    }

    public ClassNode asClassNode() {
        throw new InvalidPathException("Expected class node");
    }

    //workaround for issue: https://github.com/json-path/JsonPath/issues/613
    public boolean isOffsetDateTimeNode(){
        return false;
    }

    public OffsetDateTimeNode asOffsetDateTimeNode(){
        throw new InvalidPathException("Expected offsetDateTime node");
    }


    private static boolean isPath(Object o) {
        if(o == null || !(o instanceof String)){
            return false;
        }
        String str = o.toString().trim();
        if (str.length() <= 0) {

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Use the correct class-node syntax with the proper type reference supported by your JsonPath version, e.g. $.items[?(@.val instanceof java.lang.String)]
  2. Check the JsonPath version: type-check filter support and syntax changed across versions; use a version that supports the instanceof operator if needed
  3. Rewrite the check without type checks, e.g. verify with a regular comparison or by post-filtering in Java
  4. Catch InvalidPathException and fall back to manual type filtering in application code

Example fix

// before
$.items[?(@.val instanceof String)] // shorthand not a class node
// after
$.items[?(@.val instanceof java.lang.String)]
Defensive patterns

Strategy: validation

Validate before calling

String expr = "$.items[?(@.val instanceof java.lang.String)]";
if (!expr.matches(".*instanceof [a-z]+(\.[A-Za-z0-9_]+)+.*")) throw new IllegalArgumentException("type check needs a fully qualified class name");

Try / catch

try {
    return JsonPath.parse(json).read(expr);
} catch (InvalidPathException e) {
    // fall back to post-filtering by Java type
    return manualTypeFilter(json);
}

Prevention

When it happens

Trigger: Using the type-check operator in a filter where the operand is not a class reference — e.g. writing @.value instanceof String (a plain string) instead of a fully qualified class name/type node, or an operand path resolving to a scalar where a ClassNode was expected by evaluate.

Common situations: Writing type-check filters with shorthand class names or wrong syntax; documents where the checked value is a scalar so the operator's operand resolution fails; misuse of the internal ValueNode API.

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