{"record":{"id":"78d9ad2cab0ca05d","repo":"json-path/JsonPath","slug":"can-only-rename-properties-in-a-map","errorCode":null,"errorMessage":"Can only rename properties in a map","messagePattern":"Can only rename properties in a map","errorType":"exception","errorClass":"InvalidModificationException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java","lineNumber":68,"sourceCode":"    public abstract void convert(MapFunction mapFunction, Configuration configuration);\n\n    public abstract void delete(Configuration configuration);\n\n    public abstract void add(Object newVal, Configuration configuration);\n\n    public abstract void put(String key, Object newVal, Configuration configuration);\n\n    public abstract void renameKey(String oldKey,String newKeyName, Configuration configuration);\n\n    protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){\n        if(configuration.jsonProvider().isMap(targetMap)){\n            if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){\n                throw new PathNotFoundException(\"No results for Key \"+oldKeyName+\" found in map!\");\n            }\n            configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName));\n            configuration.jsonProvider().removeProperty(targetMap, oldKeyName);\n        } else {\n            throw new InvalidModificationException(\"Can only rename properties in a map\");\n        }\n    }\n\n    protected boolean targetInvalid(Object target){\n        return target == JsonProvider.UNDEFINED || target == null;\n    }\n\n    @Override\n    public int compareTo(PathRef o) {\n        return this.getAccessor().toString().compareTo(o.getAccessor().toString()) * -1;\n    }\n\n    public static PathRef create(Object obj, String property){\n        return new ObjectPropertyPathRef(obj, property);\n    }\n\n    public static PathRef create(Object obj, Collection<String> properties){\n        return new ObjectMultiPropertyPathRef(obj, properties);","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java#L50-L86","documentation":"renameInMap only operates on JSON objects (maps). If the resolved target is an array or a scalar, renaming a property is meaningless, so it throws InvalidModificationException with 'Can only rename properties in a map'. The library refuses to perform a structurally impossible modification.","triggerScenarios":"Calling JsonPath.renameKey with a path that resolves to an array element or a primitive value instead of an object, e.g. renaming a key at \"$.items[0]\" when that element is a string.","commonSituations":"Assuming a path points to an object while the document shape changed; targeting an array index where the desired key lives one level deeper inside each element; JSON APIs returning scalars where objects were expected.","solutions":["Check that the resolved value is a map (configuration.jsonProvider().isMap(...)) before renaming.","Adjust the path to point at the containing object, not at the array or scalar.","If the target is an array, iterate its elements and rename the key within each object element individually.","Validate the document structure against the expected schema before mutating it."],"exampleFix":"// before\njsonPath.renameKey(doc, \"$.items[0]\", \"old\", \"new\", conf); // items[0] is an array index\n// after\nfor (int i = 0; i < items.length; i++) {\n    jsonPath.renameKey(doc, \"$.items[\" + i + \"].obj\", \"old\", \"new\", conf);\n}","handlingStrategy":"type-guard","validationCode":"Object target = JsonPath.read(document, path);\nif (!(target instanceof Map)) {\n    throw new IllegalArgumentException(\"Path '\" + path + \"' must resolve to an object for renameKey, got: \"\n        + (target == null ? \"null\" : target.getClass().getSimpleName()));\n}","typeGuard":"boolean resolvesToMap(Object doc, String path, Configuration conf) {\n    try {\n        Object t = new JsonPath(path).read(doc, conf);\n        return conf.jsonProvider().isMap(t);\n    } catch (PathNotFoundException e) { return false; }\n}","tryCatchPattern":"try {\n    JsonPath.renameKey(document, path, oldKey, newKey, conf);\n} catch (InvalidModificationException e) {\n    throw new IllegalStateException(\"Path '\" + path + \"' does not resolve to a JSON object\", e);\n}","preventionTips":["Check isMap on the resolved value before any rename","For arrays, rename the key inside each element object instead","Validate document structure after upstream schema changes","Prefer paths ending at the object level (e.g. $.user.email's parent) for rename targets"],"tags":["json-path","rename-key","type-mismatch","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"}