json-path/JsonPath · error · InvalidModificationException

Invalid delete operation

Error message

Invalid delete operation

What it means

The root ('$') PathRef rejects delete: removing the entire document root is not a meaningful modification, so delete(Configuration) throws InvalidModificationException 'Invalid delete operation' unconditionally when the deletion path resolves to the root.

Source

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

        }

        @Override
        Object getAccessor() {
            return "$";
        }

        @Override
        public void set(Object newVal, Configuration configuration) {
            throw new InvalidModificationException("Invalid set operation");
        }

        public void convert(MapFunction mapFunction, Configuration configuration){
            throw new InvalidModificationException("Invalid map operation");
        }

        @Override
        public void delete(Configuration configuration) {
            throw new InvalidModificationException("Invalid delete operation");
        }

        @Override
        public void add(Object newVal, Configuration configuration) {
            if(configuration.jsonProvider().isArray(parent)){
                configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);
            } else {
                throw new InvalidModificationException("Invalid add operation. $ is not an array");
            }
        }

        @Override
        public void put(String key, Object newVal, Configuration configuration) {
            if(configuration.jsonProvider().isMap(parent)){
                configuration.jsonProvider().setProperty(parent, key, newVal);
            } else {
                throw new InvalidModificationException("Invalid put operation. $ is not a map");
            }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Delete specific sub-paths (e.g. "$.field" or "$.array[0]") instead of "$".
  2. To clear the document, replace your document reference with null or an empty object outside JsonPath.
  3. Skip paths equal to "$" when iterating over paths selected for deletion.
  4. Catch InvalidModificationException if root deletion may occur and handle it explicitly.

Example fix

// before
JsonPath.delete(document, "$", conf); // throws
// after
JsonPath.delete(document, "$.unwantedField", conf);
Defensive patterns

Strategy: validation

Validate before calling

if ("$".equals(path)) {
    throw new IllegalArgumentException("Cannot JsonPath.delete the root; null/replace the document reference instead");
}

Try / catch

try {
    JsonPath.delete(document, path, conf);
} catch (InvalidModificationException e) {
    throw new IllegalStateException("Delete at path '" + path + "' is not supported (root cannot be deleted)", e);
}

Prevention

When it happens

Trigger: Calling JsonPath.delete(document, "$", configuration) — using the root path as delete target.

Common situations: Bulk cleanup code that deletes every path in a collected list where one entry is the root; misunderstanding that delete removes children of the root rather than the root itself.

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