{"record":{"id":"33e0110db791c3ec","repo":"json-path/JsonPath","slug":"invalid-map-operation","errorCode":null,"errorMessage":"Invalid map operation","messagePattern":"Invalid map operation","errorType":"exception","errorClass":"InvalidModificationException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java","lineNumber":114,"sourceCode":"\n    private static class RootPathRef extends PathRef {\n\n        private RootPathRef(Object parent) {\n            super(parent);\n        }\n\n        @Override\n        Object getAccessor() {\n            return \"$\";\n        }\n\n        @Override\n        public void set(Object newVal, Configuration configuration) {\n            throw new InvalidModificationException(\"Invalid set operation\");\n        }\n\n        public void convert(MapFunction mapFunction, Configuration configuration){\n            throw new InvalidModificationException(\"Invalid map operation\");\n        }\n\n        @Override\n        public void delete(Configuration configuration) {\n            throw new InvalidModificationException(\"Invalid delete operation\");\n        }\n\n        @Override\n        public void add(Object newVal, Configuration configuration) {\n            if(configuration.jsonProvider().isArray(parent)){\n                configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);\n            } else {\n                throw new InvalidModificationException(\"Invalid add operation. $ is not an array\");\n            }\n        }\n\n        @Override\n        public void put(String key, Object newVal, Configuration configuration) {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java#L96-L132","documentation":"The root ('$') PathRef also rejects convert (map/MapFunction application): transforming the entire document via the root path is not a supported operation, and the implementation throws InvalidModificationException 'Invalid map operation' unconditionally.","triggerScenarios":"Calling JsonPath.map(document, \"$\", mapFunction, configuration) with the root path \"$\".","commonSituations":"Applying a MapFunction to the whole document believing it acts like a deep transform; dynamic code that maps every matched path including the root; porting per-field map code and accidentally leaving the path as \"$\".","solutions":["Apply the MapFunction to a concrete sub-path such as \"$.items[*]\" rather than \"$\".","Iterate specific fields and map each individually instead of transforming the root.","For whole-document transformation, perform it outside JsonPath (process the parsed object directly).","Filter out root matches (\"$\") in dynamic path-processing loops."],"exampleFix":"// before\nJsonPath.map(document, \"$\", ctx -> upperCaseAll(ctx), conf); // throws\n// after\nJsonPath.map(document, \"$.names[*]\", ctx -> ctx.toString().toUpperCase(), conf);","handlingStrategy":"validation","validationCode":"if (\"$\".equals(path)) {\n    throw new IllegalArgumentException(\"Cannot JsonPath.map the root; transform concrete sub-paths instead\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    JsonPath.map(document, path, mapFunction, conf);\n} catch (InvalidModificationException e) {\n    throw new IllegalStateException(\"Map at path '\" + path + \"' is not supported (root cannot be mapped)\", e);\n}","preventionTips":["Apply MapFunctions to leaf or wildcard sub-paths like $.items[*]","Transform the whole document by post-processing the parsed object directly","Filter \"$\" out of dynamically collected path lists before mapping","Remember root-level set/map/delete are all intentionally unsupported"],"tags":["json-path","map-operation","root-path","invalid-modification"],"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"}