{"record":{"id":"ea60719e10275fb9","repo":"json-path/JsonPath","slug":"jsonarray-is-immutable-in-json-p","errorCode":null,"errorMessage":"JsonArray is immutable in JSON-P","messagePattern":"JsonArray is immutable in JSON-P","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java","lineNumber":162,"sourceCode":"        } else {\r\n            throw new UnsupportedOperationException();\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void setArrayIndex(Object array, int index, Object newValue) {\r\n        if (array instanceof JsonArrayBuilder) {\r\n            // next line is not optimal, but ArrayBuilder has no size() method\r\n            if (index == ((JsonArrayBuilder) array).build().size()) {\r\n                array = ((JsonArrayBuilder) array).add(wrap(newValue));\r\n            } else {\r\n                array = ((JsonArrayBuilder) array).set(index, wrap(newValue));\r\n            }\r\n        } else if (array instanceof JsonArray) {\r\n        \tif (mutableJson && array instanceof JsonArrayProxy) {\r\n        \t\t((JsonArrayProxy) array).set(index, wrap(newValue));\r\n        \t} else {\r\n        \t\tthrow new UnsupportedOperationException(\"JsonArray is immutable in JSON-P\");\r\n        \t}\r\n        } else {\r\n            super.setArrayIndex(array, index, wrap(newValue));\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public Object getMapValue(Object obj, String key) {\r\n        if (obj instanceof JsonObjectBuilder) {\r\n            obj = ((JsonObjectBuilder) obj).build();\r\n        }\r\n        if (obj instanceof JsonObject) {\r\n            JsonValue o = ((JsonObject) obj).get(key);\r\n            if (o == null) {\r\n                return UNDEFINED;\r\n            } else {\r\n                return unwrap(o);\r\n            }\r","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java#L144-L180","documentation":"JSON-P JsonArray instances are immutable; JakartaJsonProvider.setArrayIndex throws UnsupportedOperationException when asked to mutate a plain JsonArray that is not a mutable proxy (mutableJson disabled). JsonPath mutation only works with JsonArrayBuilder, a JsonArrayProxy (when mutableJson is on), or other List types.","triggerScenarios":"Calling setArrayIndex (directly or via JsonPath.set/put operations) on a document parsed with JakartaJsonProvider where the target node is a JsonArray and mutableJson is false and the array is not a JsonArrayProxy.","commonSituations":"Using JsonPath.set(...) to modify an array element in a JSON-P parsed document without enabling the provider's mutableJson mode.","solutions":["Configure the JakartaJsonProvider with mutableJson = true so parsed structures are wrapped in mutable proxies.","Convert the JsonArray to a JsonArrayBuilder (Json.createArrayBuilder(array)), mutate, then build.","Parse the document into plain Java collections (List/Map) instead of JSON-P types if you need in-place mutation."],"exampleFix":"// before\nConfiguration conf = Configuration.builder().jsonProvider(new JakartaJsonProvider()).build();\n// after\nConfiguration conf = Configuration.builder().jsonProvider(new JakartaJsonProvider(true)).build(); // mutableJson enabled","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Java\nstatic boolean isArrayMutable(Object a, boolean mutableJson) {\n    return a instanceof JsonArrayBuilder || a instanceof java.util.List\n        || (mutableJson && a instanceof JsonArrayProxy);\n}","tryCatchPattern":"try { provider.setArrayIndex(arr, i, v); } catch (UnsupportedOperationException e) { /* convert to JsonArrayBuilder and retry */ }","preventionTips":["Enable mutableJson when documents must be modified","Use Json.createArrayBuilder for edits on immutable arrays","Prefer map/list-based providers for write-heavy workloads"],"tags":["json-p","immutability","unsupported-operation","mutation"],"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"}