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
- Use a definite (non-wildcard) path that resolves to exactly one property before calling add().
- 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).
- If the intent is fan-out mutation, consider MapFunction/transform with JsonActions instead of add().
- 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
- Never call add()/put() with wildcard, deep-scan, or filter paths.
- Resolve matches first (path() / paths) and mutate each definite path in a loop.
- Reserve mutation operations for definite paths only.
- Document this constraint in team guidelines for JsonPath usage.
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
- Can only add to an array
- Can only add properties to a map
- Put can not be performed to multiple properties
- Rename can not be performed to multiple properties
- Invalid array index
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/13c3105c4aea45d1.
Report an issue: GitHub.