json-path/JsonPath · error · JsonPathException

setProperty operation cannot be used with

Error message

setProperty operation cannot be used with 

What it means

AbstractJsonProvider.setProperty mutates an object by putting a key/value into a JSON object (Map). If the target object is not a Map, the provider cannot set a property and throws JsonPathException. Note the message has an operator-precedence bug ('+' binds tighter than '!='), so the text may be garbled, but the cause is always a non-object target.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java:93

            return JsonProvider.UNDEFINED;
        } else {
            return m.get(key);
        }
    }

    /**
     * Sets a value in an object
     *
     * @param obj   an object
     * @param key   a String key
     * @param value the value to set
     */
    @SuppressWarnings("unchecked")
    public void setProperty(Object obj, Object key, Object value) {
        if (isMap(obj))
            ((Map) obj).put(key.toString(), value);
        else {
            throw new JsonPathException("setProperty operation cannot be used with " + obj!=null?obj.getClass().getName():"null");
        }
    }



    /**
     * Removes a value in an object or array
     *
     * @param obj   an array or an object
     * @param key   a String key or a numerical index to remove
     */
    @SuppressWarnings("unchecked")
    public void removeProperty(Object obj, Object key) {
        if (isMap(obj))
            ((Map) obj).remove(key.toString());
        else {
            List list = (List) obj;
            int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Verify the target is a JSON object before calling setProperty (jsonProvider.isMap(obj))
  2. Convert the target to an object first or correct the path so it points at an object node
  3. For arrays/strings, use the appropriate provider operations (array add, length, etc.) instead of property set
  4. Guard against null targets explicitly before mutation

Example fix

// before
provider.setProperty(target, "key", value); // throws when target is a List
// after
if (provider.isMap(target)) {
    provider.setProperty(target, "key", value);
} else {
    throw new IllegalArgumentException("target is not a JSON object");
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj == null || !provider.isMap(obj)) throw new IllegalArgumentException("setProperty target must be a JSON object");

Type guard

boolean canSetProperty(Object o) { return o instanceof Map<?, ?>; }

Try / catch

try {
    provider.setProperty(obj, key, value);
} catch (JsonPathException e) {
    // obj was not a JSON object — handle non-object branch
}

Prevention

When it happens

Trigger: Calling Configuration.defaultConfiguration().jsonProvider().setProperty(listOrString, key, value); using JsonPath's mapFunction/put-style helpers (e.g. using(JsonPath).put(...)/ParseContext set operations) against a JSON array, string, or number instead of an object; passing null as the target object.

Common situations: Document-generation code that assumes a node is an object but the parsed JSON has it as an array or scalar; mutating documents where the path resolved to the wrong node; generic JSON transformation pipelines applying object updates to every node type.

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/a63900d502c53c92. Report an issue: GitHub.