{"record":{"id":"49e4c5808a322060","repo":"json-path/JsonPath","slug":"filter-s-can-not-be-applied-to-primitives-curre","errorCode":null,"errorMessage":"Filter: %s can not be applied to primitives. Current context is: %s","messagePattern":"Filter: (.+?) can not be applied to primitives\\. Current context is: (.+?)","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/path/PredicatePathToken.java","lineNumber":67,"sourceCode":"                if (isLeaf()) {\n                    ctx.addResult(currentPath, op, model);\n                } else {\n                    next().evaluate(currentPath, op, model, ctx);\n                }\n            }\n        } else if (ctx.jsonProvider().isArray(model)){\n            int idx = 0;\n            Iterable<?> objects = ctx.jsonProvider().toIterable(model);\n\n            for (Object idxModel : objects) {\n                if (accept(idxModel, ctx.rootDocument(),  ctx.configuration(), ctx)) {\n                    handleArrayIndex(idx, currentPath, model, ctx);\n                }\n                idx++;\n            }\n        } else {\n            if (isUpstreamDefinite()) {\n                throw new InvalidPathException(format(\"Filter: %s can not be applied to primitives. Current context is: %s\", toString(), model));\n            }\n        }\n    }\n\n    public boolean accept(final Object obj, final Object root, final Configuration configuration, EvaluationContextImpl evaluationContext) {\n        Predicate.PredicateContext ctx = new PredicateContextImpl(obj, root, configuration, evaluationContext.documentEvalCache());\n\n        for (Predicate predicate : predicates) {\n            try {\n                if (!predicate.apply(ctx)) {\n                    return false;\n                }\n            } catch (InvalidPathException e) {\n                return false;\n            }\n        }\n        return true;\n    }","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/path/PredicatePathToken.java#L49-L85","documentation":"InvalidPathException thrown by PredicatePathToken.evaluate when a filter [?(...)] is applied to a primitive (non-array, non-object) value in the current context and the upstream path is definite. Filters only make sense over collections/objects; applying one to a string, number, or boolean is a path misuse.","triggerScenarios":"Evaluating a path like \"$.name[?(@ == 'x')]\" where $.name resolves to a string, or applying a filter at a path position that selected a scalar instead of an array/object.","commonSituations":"Wrong path depth (filter one level too deep into a scalar field), document schema change turning an array into a single value, copy-pasted filter paths applied to the wrong node.","solutions":["Move the filter up one path level so it applies to the array/object containing the primitive","Verify with a plain read that the pre-filter path resolves to an array or object, not a scalar","Update the path if the document schema changed from array to scalar","Use a filter on the parent: e.g. \"$[?(@.name == 'x')]\" instead of \"$.name[?(@ == 'x')]\""],"exampleFix":"// before\nJsonPath.read(json, \"$.items.name[?(@ == 'widget')]\")\n// after\nJsonPath.read(json, \"$.items[?(@.name == 'widget')]\")","handlingStrategy":"validation","validationCode":"DocumentContext ctx = JsonPath.parse(json);\nObject pre = ctx.read(\"$.items\");\nif (!(pre instanceof List) && !(pre instanceof Map)) {\n    throw new IllegalArgumentException(\"Filter target is a primitive; apply the filter one level up\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.read(json, filteredPath);\n} catch (InvalidPathException e) {\n    if (e.getMessage().contains(\"can not be applied to primitives\")) {\n        throw new IllegalArgumentException(\"Filter applied to scalar at: \" + filteredPath);\n    }\n    throw e;\n}","preventionTips":["Place filters on array/object nodes: $[?(@.x == 1)], not $.x[?(@ == 1)]","Probe the unfiltered path first and assert it resolves to a List/Map","Re-check filter paths after schema changes that convert arrays to scalars"],"tags":["json-path","filter","predicate","type-mismatch"],"backgroundTag":"unsupported-operation","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"}