{"record":{"id":"b89a949bd7ce3554","repo":"json-path/JsonPath","slug":"could-not-convert","errorCode":null,"errorMessage":"Could not convert ","messagePattern":"Could not convert ","errorType":"exception","errorClass":"JsonPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java","lineNumber":705,"sourceCode":"                    Object res;\n                    if (ctx instanceof PredicateContextImpl) {\n                        //This will use cache for document ($) queries\n                        PredicateContextImpl ctxi = (PredicateContextImpl) ctx;\n                        res = ctxi.evaluate(path);\n                    } else {\n                        Object doc = path.isRootPath() ? ctx.root() : ctx.item();\n                        res = path.evaluate(doc, ctx.root(), ctx.configuration()).getValue();\n                    }\n                    res = ctx.configuration().jsonProvider().unwrap(res);\n\n                    if (res instanceof Number) return ValueNode.createNumberNode(res.toString());\n                    else if (res instanceof String) return ValueNode.createStringNode(res.toString(), false);\n                    else if (res instanceof Boolean) return ValueNode.createBooleanNode(res.toString());\n                    else if (res instanceof OffsetDateTime) return ValueNode.createOffsetDateTimeNode(res.toString()); //workaround for issue: https://github.com/json-path/JsonPath/issues/613\n                    else if (res == null) return NULL_NODE;\n                    else if (ctx.configuration().jsonProvider().isArray(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, List.class, ctx.configuration()));\n                    else if (ctx.configuration().jsonProvider().isMap(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, Map.class, ctx.configuration()));\n                    else throw new JsonPathException(\"Could not convert \" + res.getClass().toString()+\":\"+ res.toString() + \" to a ValueNode\");\n                } catch (PathNotFoundException e) {\n                    return UNDEFINED;\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":687,"sourceCodeEnd":713,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java#L687-L713","documentation":"When evaluating a filter ValueNode created from a path (PathJsonPathValueNode-style), the library evaluates the sub-path against the document and converts the result into a ValueNode (String, Boolean, JsonNode, etc.). If the result's type is none of the supported shapes (Number/String/Boolean/OffsetDateTime/null/array/map), it throws JsonPathException('Could not convert ... to a ValueNode').","triggerScenarios":"A filter operand path resolves to an unexpected runtime type — typically a Number (older versions without a numeric branch) or a custom object returned by a MappingProvider — inside a predicate such as [?(@.a.b == 3)] where @.a.b evaluates to an Integer/Double not handled by the conversion chain.","commonSituations":"Comparing against numeric fields with older json-path versions (numbers were not convertible), using a custom MappingProvider/Configuration that returns domain objects instead of primitives/maps/lists, or documents parsed with a provider producing non-standard types.","solutions":["Upgrade json-path to a version whose ValueNode supports numeric conversion, or ensure the compared value is a String/Boolean/List/Map.","Configure Configuration with a MappingProvider that maps values to standard Java types (e.g. JacksonMappingProvider).","Cast the comparison operand to a supported type in the predicate, e.g. compare strings instead of raw numbers.","Catch JsonPathException and fall back to evaluating the predicate in application code."],"exampleFix":"// before\nJsonPath.parse(json).set(\"$[?(@.count == 5)].status\", \"ok\"); // count maps to Integer -> not convertible (old versions)\n// after\nConfiguration cfg = Configuration.builder().mappingProvider(new JacksonMappingProvider()).build();\nJsonPath.using(cfg).parse(json).set(\"$[?(@.count == 5)].status\", \"ok\");","handlingStrategy":"type-guard","validationCode":"Object v = JsonPath.read(json, operandPath);\nif (!(v instanceof String || v instanceof Boolean || v instanceof List || v instanceof Map || v == null)) {\n    throw new IllegalStateException(\"Operand type not convertible to ValueNode: \" + v.getClass());\n}","typeGuard":"boolean isValueNodeConvertible(Object v) {\n    return v == null || v instanceof String || v instanceof Boolean\n        || v instanceof List || v instanceof Map;\n}","tryCatchPattern":"try {\n    JsonPath.read(json, path);\n} catch (JsonPathException e) {\n    if (e.getMessage().startsWith(\"Could not convert\")) { /* handle type mismatch */ }\n}","preventionTips":["Use a standard MappingProvider (Jackson/Gson) so values map to plain types","Avoid predicates comparing raw numeric operands on old json-path versions","Pin json-path to a recent version with numeric ValueNode support"],"tags":["json","type-conversion","path-expression"],"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"}