{"record":{"id":"ddfaae8ec68ca4d7","repo":"json-path/JsonPath","slug":"jsonobject-is-immutable-in-json-p","errorCode":null,"errorMessage":"JsonObject is immutable in JSON-P","messagePattern":"JsonObject 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":193,"sourceCode":"            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\n        } else {\r\n            throw new UnsupportedOperationException();\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void setProperty(Object obj, Object key, Object value) {\r\n        if (obj instanceof JsonObjectBuilder) {\r\n            ((JsonObjectBuilder) obj).add(key.toString(), wrap(value));\r\n        } else if (mutableJson && obj instanceof JsonObjectProxy) {\r\n    \t\t((JsonObjectProxy) obj).put(key.toString(), wrap(value));\r\n    \t} else if (obj instanceof JsonObject) {\r\n    \t\tthrow new UnsupportedOperationException(\"JsonObject is immutable in JSON-P\");\r\n    \t} else if (obj instanceof JsonArrayBuilder) {\r\n    \t\tif (key == null) {\r\n    \t\t\t((JsonArrayBuilder) obj).add(wrap(value));\r\n    \t\t} else {\r\n\t\t\t\t((JsonArrayBuilder) obj).set(toArrayIndex(key), wrap(value));\r\n    \t\t}\r\n        } else if (mutableJson && obj instanceof JsonArrayProxy) {\r\n        \tif (key == null) {\r\n    \t\t\t((JsonArrayProxy) obj).add(wrap(value));\r\n        \t} else {\r\n        \t\t((JsonArrayProxy) obj).set(toArrayIndex(key), wrap(value));\r\n    \t\t}\r\n    \t} else if (obj instanceof JsonArray) {\r\n    \t\tthrow new UnsupportedOperationException(\"JsonArray is immutable in JSON-P\");\r\n        } else if (obj instanceof List) {\r\n        \t@SuppressWarnings(\"unchecked\")\r\n        \tList<JsonValue> array = (List<JsonValue>) obj;\r\n        \tif (key == null) {\r","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java#L175-L211","documentation":"JSON-P JsonObject is immutable, so JakartaJsonProvider.setProperty throws UnsupportedOperationException when the target is a plain JsonObject that is neither a JsonObjectBuilder nor (in mutableJson mode) a JsonObjectProxy. Setting properties on such objects is impossible without converting to a builder.","triggerScenarios":"JsonPath.put/set operations targeting an object member where the node is a JsonObject, mutableJson is false, and it is not a JsonObjectProxy.","commonSituations":"Attempting to add or overwrite fields in a JSON-P parsed document with the default (immutable) provider configuration.","solutions":["Enable mutableJson on the provider so documents are parsed into mutable proxies.","Convert with Json.createObjectBuilder(jsonObject), mutate the builder, then build().","Use a provider based on plain Maps (Jackson/Gson/JacksonJsonNodeJsonProvider) when mutation is required."],"exampleFix":"// before\njsonPath.put(document, \"newKey\", value); // throws on immutable JsonObject\n// after\nJsonObjectBuilder b = Json.createObjectBuilder((JsonObject) document);\nb.add(\"newKey\", value);\ndocument = b.build();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Java\nstatic boolean isObjectMutable(Object o, boolean mutableJson) {\n    return o instanceof JsonObjectBuilder || (mutableJson && o instanceof JsonObjectProxy);\n}","tryCatchPattern":"try { provider.setProperty(obj, key, value); } catch (UnsupportedOperationException e) { obj = Json.createObjectBuilder((JsonObject) obj).add(key.toString(), value).build(); }","preventionTips":["Enable mutableJson for mutation use cases","Convert immutable JsonObject via createObjectBuilder before edits","Document that JSON-P objects are read-only by design"],"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"}