json-path/JsonPath · error · InvalidPathException

Expected regexp node

Error message

Expected regexp node

What it means

ValueNode.asPatternNode() is the default (non-PatternNode) implementation on ValueNode: only nodes that actually wrap a regex (PatternNode) can be converted, and every other node type throws InvalidPathException("Expected regexp node"). It signals a type error during filter evaluation — the code expected the operand of a =~ comparison to be a regular-expression node but got a different ValueNode.

Source

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

import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.path.PathCompiler;
import net.minidev.json.parser.JSONParser;

import java.time.OffsetDateTime;
import java.util.regex.Pattern;

import static com.jayway.jsonpath.internal.filter.ValueNodes.*;

public abstract class ValueNode {

    public abstract Class<?> type(Predicate.PredicateContext ctx);

    public boolean isPatternNode() {
        return false;
    }

    public PatternNode asPatternNode() {
        throw new InvalidPathException("Expected regexp node");
    }

    public boolean isPathNode() {
        return false;
    }

    public PathNode asPathNode() {
        throw new InvalidPathException("Expected path node");
    }

    public boolean isNumberNode() {
        return false;
    }

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

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Check isPatternNode() before calling asPatternNode() in custom evaluation code, and branch to an error path otherwise.
  2. Write the regex operand as an inline pattern node (e.g. /pattern/ ) so the parser produces a PatternNode rather than a string node.
  3. If comparing against a stored string, compare with == instead of =~, or wrap the value in a PatternNode when building filter nodes programmatically.
  4. Verify the Jayway version: regex literal parsing has evolved; upgrade if your expression syntax is from an older/newer dialect.

Example fix

// before
ValueNode node = new ValueNode.StringNode("^a.*z$");
node.asPatternNode(); // throws

// after
ValueNode node = new ValueNode.PatternNode(Pattern.compile("^a.*z$"));
if (node.isPatternNode()) {
    node.asPatternNode();
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Java: verify regex literals are written as /pattern/ so they parse as PatternNodes
void validateRegexOperand(String filter) {
    if (filter.contains("=~") && !java.util.regex.Pattern.compile("=~\\s*/[^/]+/").matcher(filter).find()) {
        throw new IllegalArgumentException("=~ operand must be an inline /regex/ in: " + filter);
    }
}

Type guard

boolean asPatternSafe(ValueNode node) {
    return node.isPatternNode(); // only then call node.asPatternNode()
}

Try / catch

try {
    return node.asPatternNode();
} catch (InvalidPathException e) {
    if (e.getMessage().equals("Expected regexp node")) {
        throw new IllegalStateException("=~ requires a regex PatternNode, got " + node.getClass().getSimpleName(), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Evaluating a filter where evaluate() calls asPatternNode() on the operand of a =~ operator but the parsed operand is a string/number/path node instead of a regex pattern; programmatically building a ValueNode.RelationalNode with a =~ operator and a non-pattern operand.

Common situations: Writing /regex/ without the pattern-node syntax so the parser treats it as a plain string node; dynamically constructed filters where the regex operand is passed as a quoted string; version differences in how the Jayway parser recognizes inline regex literals.

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