{"record":{"id":"d0924b6d5e9247e7","repo":"json-path/JsonPath","slug":"expected-string-node","errorCode":null,"errorMessage":"Expected string node","messagePattern":"Expected string node","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java","lineNumber":48,"sourceCode":"\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\n    public boolean isJsonNode() {\n        return false;\n    }\n\n    public JsonNode asJsonNode() {\n        throw new InvalidPathException(\"Expected json node\");\n    }\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java#L30-L66","documentation":"In JsonPath, ValueNode is a sealed-style base type whose default asStringNode() implementation throws InvalidPathException(\"Expected string node\"). It is thrown when filter evaluation (evaluate/getInput) calls asStringNode() on a node that is not actually a StringNode — e.g. a NumberNode, BooleanNode, or JsonNode produced by evaluating a path on the wrong document shape. It signals a type mismatch between what the filter operator expected (a string operand) and what the JSON document actually contained.","triggerScenarios":"Using string comparison operators (==, =~, in, like, etc.) in a JSONPath filter where the evaluated operand is not a string, e.g. $.items[?(@.price == 'free')] where price is a number, or a path operand resolving to null/boolean instead of a string. Also calling StringNode.asStringNode() via evaluate/getInput on a subclass node.","commonSituations":"Filter predicates written against documents where field types vary (one record has \"name\": \"x\", another has name: null); comparing numeric fields to quoted string literals; migrating documents from loosely typed sources where an expected string field arrives as a number or object.","solutions":["Change the filter to match the actual JSON type, e.g. compare numbers without quotes: $.items[?(@.price == 0)] instead of == '0'","Check for null/non-string values in the document and normalize them before filtering, or guard with a nested predicate like $.items[?(@.name && @.name == 'x')]","Cast the value in the document (coerce numbers to strings at serialization time) so operands are consistent","Catch InvalidPathException around the evaluation and treat the mismatching item as a non-match"],"exampleFix":"// before\nList<Map<String,Object>> hits = JsonPath.parse(json).read(\"$.items[?(@.code == '123')]\"); // code is a number\n// after\nList<Map<String,Object>> hits = JsonPath.parse(json).read(\"$.items[?(@.code == 123)]\");","handlingStrategy":"type-guard","validationCode":"Object v = JsonPath.parse(json).read(\"$.item.price\");\nif (v == null || !(v instanceof String)) throw new IllegalArgumentException(\"price must be a string for this filter\");","typeGuard":"static boolean isStringOperand(Object o) { return o instanceof String; }","tryCatchPattern":"try {\n    return JsonPath.parse(json).read(\"$.items[?(@.code == '123')]\");\n} catch (InvalidPathException e) {\n    // operand type mismatch: treat as non-match or re-run with corrected literal\n    return Collections.emptyList();\n}","preventionTips":["Match filter literal types to the JSON types actually stored (no quotes around numbers/booleans)","Inspect a sample document with JsonPath.read on the target field before building filters","Normalize field types at document ingestion time (string vs number vs boolean)","Guard predicates against null/missing fields with nested conditions","Pin your json-path version and test filters against representative documents"],"tags":["jsonpath","filter","type-mismatch","string-node"],"backgroundTag":"type-mismatch","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}