{"record":{"id":"278f75622add60a6","repo":"json-path/JsonPath","slug":"expected-number-node","errorCode":null,"errorMessage":"Expected number node","messagePattern":"Expected number node","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java","lineNumber":40,"sourceCode":"\n    public PatternNode asPatternNode() {\n        throw new InvalidPathException(\"Expected regexp node\");\n    }\n\n    public boolean isPathNode() {\n        return false;\n    }\n\n    public PathNode asPathNode() {\n        throw new InvalidPathException(\"Expected path node\");\n    }\n\n    public boolean isNumberNode() {\n        return false;\n    }\n\n    public NumberNode asNumberNode() {\n        throw new InvalidPathException(\"Expected number node\");\n    }\n\n    public boolean isStringNode() {\n        return false;\n    }\n\n    public StringNode asStringNode() {\n        throw new InvalidPathException(\"Expected string node\");\n    }\n\n    public boolean isBooleanNode() {\n        return false;\n    }\n\n    public BooleanNode asBooleanNode() {\n        throw new InvalidPathException(\"Expected boolean node\");\n    }\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java#L22-L58","documentation":"ValueNode.asNumberNode() is the default implementation inherited by all non-numeric ValueNodes; only NumberNode can be converted. Filter evaluation code (e.g. evaluate() for size/numeric comparisons, expectedSize()) calls it when a numeric operand is required and throws InvalidPathException(\"Expected number node\") if the operand is a string or other literal node.","triggerScenarios":"Using the size operator with a non-numeric right operand (e.g. size '3' instead of size 3), or a numeric comparison whose operand the parser classified as a string/boolean node, so evaluate()/expectedSize() call asNumberNode() on the wrong node type.","commonSituations":"Quoting numbers in filters ([?(@.size == '3')]) and then using them where a numeric node is required; dynamically generated filters where sizes/counts are interpolated as strings; custom evaluation code assuming both operands are NumberNodes.","solutions":["Write numeric operands unquoted so the parser produces a NumberNode (size 3, not size '3').","Check isNumberNode() before calling asNumberNode() and convert string nodes explicitly (e.g. parse the string to a number and build a NumberNode).","When building filter nodes programmatically, use ValueNode.NumberNode (or NumberNode.of(...)) for numeric values instead of string literals.","Log the failing node's class at the call site to confirm which literal type was supplied."],"exampleFix":"// before\nValueNode node = new ValueNode.StringNode(\"3\");\nnode.asNumberNode(); // throws\n\n// after\nValueNode node = new ValueNode.NumberNode(3);\nif (node.isNumberNode()) {\n    node.asNumberNode();\n}","handlingStrategy":"type-guard","validationCode":"// Java: numeric operands in filters (e.g. size) must be unquoted integers\nvoid validateNumericOperand(String filter) {\n    java.util.regex.Matcher m = java.util.regex.Pattern.compile(\"size\\\\s+(\\\\S+)\").matcher(filter);\n    while (m.find()) {\n        if (!m.group(1).matches(\"\\\\d+\")) {\n            throw new IllegalArgumentException(\"size requires an unquoted number, got: \" + m.group(1));\n        }\n    }\n}","typeGuard":"boolean asNumberSafe(ValueNode node) {\n    return node.isNumberNode(); // only then call node.asNumberNode()\n}","tryCatchPattern":"try {\n    return node.asNumberNode();\n} catch (InvalidPathException e) {\n    if (e.getMessage().equals(\"Expected number node\")) {\n        throw new IllegalStateException(\"Expected a NumberNode, got \" + node.getClass().getSimpleName(), e);\n    }\n    throw e;\n}","preventionTips":["Write numeric filter operands unquoted: size 3, not size '3'.","Check isNumberNode() before asNumberNode().","Convert user-supplied numbers explicitly with NumberNode instead of string interpolation.","Validate generated filters with a parse test before evaluation."],"tags":["json-path","filter-evaluation","number-node","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}