{"record":{"id":"68e765967e00782c","repo":"json-path/JsonPath","slug":"invalid-set-operation","errorCode":null,"errorMessage":"Invalid set operation","messagePattern":"Invalid set operation","errorType":"exception","errorClass":"InvalidModificationException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java","lineNumber":110,"sourceCode":"\n    public static PathRef createRoot(Object root){\n        return new RootPathRef(root);\n    }\n\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            }","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java#L92-L128","documentation":"The '$' (root) PathRef implementation intentionally rejects set: the JSON root can only hold a map or array container, and assigning a scalar replacement to the entire document is not a supported modification. Any set call that resolves to the root throws InvalidModificationException 'Invalid set operation'.","triggerScenarios":"Calling JsonPath.set(document, \"$\", newVal, configuration) — i.e. using the root path \"$\" as the set target.","commonSituations":"Trying to replace the whole document via set; generic code that builds paths dynamically and degenerates to \"$\"; misusing set where map/array put or add operations were intended.","solutions":["Set a concrete sub-path (e.g. \"$.field\") instead of the root path.","To replace the whole document, assign the new value to your document variable directly instead of using JsonPath.set.","If you only need to change part of the document, use put/add on the appropriate container path.","Guard against path==\"$\" in code that computes paths dynamically."],"exampleFix":"// before\nJsonPath.set(document, \"$\", newRoot, conf); // throws\n// after\ndocument = newRoot; // replace reference, or:\nJsonPath.set(document, \"$.field\", newRoot, conf);","handlingStrategy":"validation","validationCode":"if (\"$\".equals(path)) {\n    throw new IllegalArgumentException(\"Cannot JsonPath.set the root; assign the document reference instead\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    JsonPath.set(document, path, newVal, conf);\n} catch (InvalidModificationException e) {\n    throw new IllegalStateException(\"Set at path '\" + path + \"' is not supported (root cannot be set)\", e);\n}","preventionTips":["Never use \"$\" as a set target; pick a concrete sub-path","Replace the document variable itself to swap the whole document","In dynamic path pipelines, skip or special-case root matches","Use put/add for container-level changes instead of set"],"tags":["json-path","set-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"}