json-path/JsonPath · error · InvalidModificationException

Add can not be performed to multiple properties

Error message

Add can not be performed to multiple properties

What it means

The multi-property PathRef variant represents a path that matched more than one property at once (e.g. a wildcard like `$..*` or `$.obj.*`). Jayway JsonPath deliberately refuses add()/put() on such refs because there is no single unambiguous destination for the new value, so it unconditionally throws InvalidModificationException("Add can not be performed to multiple properties").

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java:315

        }
        public void convert(MapFunction mapFunction, Configuration configuration) {
            for (String property : properties) {
                Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
                if (currentValue != JsonProvider.UNDEFINED) {
                    configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration));
                }
            }
        }

        public void delete(Configuration configuration){
            for (String property : properties) {
                configuration.jsonProvider().removeProperty(parent, property);
            }
        }

        @Override
        public void add(Object newVal, Configuration configuration) {
            throw new InvalidModificationException("Add can not be performed to multiple properties");
        }

        @Override
        public void put(String key, Object newVal, Configuration configuration) {
            throw new InvalidModificationException("Put can not be performed to multiple properties");
        }

        @Override
        public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {
            throw new InvalidModificationException("Rename can not be performed to multiple properties");
        }

        @Override
        public Object getAccessor() {
            return Utils.join("&&", properties);
        }
    }
}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Use a definite (non-wildcard) path that resolves to exactly one property before calling add().
  2. Enumerate the matches yourself with read(path) returning a list/map, then add()/put() each concrete path in a loop (e.g. via pathsWithDef or map backwards API).
  3. If the intent is fan-out mutation, consider MapFunction/transform with JsonActions instead of add().
  4. Catch InvalidModificationException and fall back to per-match mutation.

Example fix

// before
context.add("$..items", extraItem); // throws: matches multiple properties
// after
List<String> paths = context.read("$..items.path()"); // or paths from listener
for (String p : paths) {
    context.add(p, extraItem);
}
Defensive patterns

Strategy: fallback

Validate before calling

List<String> definitePaths = context.read(wildcardPath + ".path()");
if (definitePaths.size() != 1) throw new IllegalStateException("path must resolve to exactly one property for add()");

Try / catch

try { context.add(path, value); } catch (InvalidModificationException e) { // wildcard/multi-match: iterate concrete paths instead
    for (String p : context.read(path + ".path()")) { context.add(p, value); } }

Prevention

When it happens

Trigger: Calling JsonPath.parse(...).add(path, value) where the path contains wildcards, deep-scan (`..`), filters, or otherwise evaluates to multiple properties — the resulting PathRef is the multi-property variant whose add() always throws.

Common situations: Using wildcard or filter paths in mutations (`$.*.tags`, `$..items`) assuming Jayway fans out the add to each match (it does not for add/put); upgrading code from read-only wildcard queries to mutations.

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