json-path/JsonPath · error · UnsupportedOperationException

JsonArray is immutable in JSON-P

Error message

JsonArray is immutable in JSON-P

What it means

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.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java:162

        } else {
            throw new UnsupportedOperationException();
        }
    }

    @Override
    public void setArrayIndex(Object array, int index, Object newValue) {
        if (array instanceof JsonArrayBuilder) {
            // next line is not optimal, but ArrayBuilder has no size() method
            if (index == ((JsonArrayBuilder) array).build().size()) {
                array = ((JsonArrayBuilder) array).add(wrap(newValue));
            } else {
                array = ((JsonArrayBuilder) array).set(index, wrap(newValue));
            }
        } else if (array instanceof JsonArray) {
        	if (mutableJson && array instanceof JsonArrayProxy) {
        		((JsonArrayProxy) array).set(index, wrap(newValue));
        	} else {
        		throw new UnsupportedOperationException("JsonArray is immutable in JSON-P");
        	}
        } else {
            super.setArrayIndex(array, index, wrap(newValue));
        }
    }

    @Override
    public Object getMapValue(Object obj, String key) {
        if (obj instanceof JsonObjectBuilder) {
            obj = ((JsonObjectBuilder) obj).build();
        }
        if (obj instanceof JsonObject) {
            JsonValue o = ((JsonObject) obj).get(key);
            if (o == null) {
                return UNDEFINED;
            } else {
                return unwrap(o);
            }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Configure the JakartaJsonProvider with mutableJson = true so parsed structures are wrapped in mutable proxies.
  2. Convert the JsonArray to a JsonArrayBuilder (Json.createArrayBuilder(array)), mutate, then build.
  3. Parse the document into plain Java collections (List/Map) instead of JSON-P types if you need in-place mutation.

Example fix

// before
Configuration conf = Configuration.builder().jsonProvider(new JakartaJsonProvider()).build();
// after
Configuration conf = Configuration.builder().jsonProvider(new JakartaJsonProvider(true)).build(); // mutableJson enabled
Defensive patterns

Strategy: type-guard

Type guard

// Java
static boolean isArrayMutable(Object a, boolean mutableJson) {
    return a instanceof JsonArrayBuilder || a instanceof java.util.List
        || (mutableJson && a instanceof JsonArrayProxy);
}

Try / catch

try { provider.setArrayIndex(arr, i, v); } catch (UnsupportedOperationException e) { /* convert to JsonArrayBuilder and retry */ }

Prevention

When it happens

Trigger: 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.

Common situations: Using JsonPath.set(...) to modify an array element in a JSON-P parsed document without enabling the provider's mutableJson mode.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/ea60719e10275fb9. Report an issue: GitHub.