json-path/JsonPath · error · JsonPathException

length operation cannot be applied to

Error message

length operation cannot be applied to 

What it means

AbstractJsonProvider.length returns the size of arrays (List), objects (Map), or strings. For any other node type — numbers, booleans, null — there is no meaningful length, so a JsonPathException is thrown. This surfaces when a JSON-path length() function is applied to a non-sized node.

Source

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

            return ((Map) obj).keySet();
        }
    }

    /**
     * Get the length of an array or object
     *
     * @param obj an array or an object
     * @return the number of entries in the array or object
     */
    public int length(Object obj) {
        if (isArray(obj)) {
            return ((List) obj).size();
        } else if (isMap(obj)){
            return getPropertyKeys(obj).size();
        } else if(obj instanceof String){
            return ((String)obj).length();
        }
        throw new JsonPathException("length operation cannot be applied to " + (obj != null ? obj.getClass().getName()
                : "null"));
    }

    /**
     * Converts given array to an {@link Iterable}
     *
     * @param obj an array
     * @return an Iterable that iterates over the entries of an array
     */
    @SuppressWarnings("unchecked")
    public Iterable<?> toIterable(Object obj) {
        if (isArray(obj))
            return ((Iterable) obj);
        else
            throw new JsonPathException("Cannot iterate over " + obj!=null?obj.getClass().getName():"null");
    }

    @Override

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Ensure the node the length() function is applied to is an array, object, or string
  2. Null-check / type-check the resolved value before applying length: read the node first and test its type
  3. Adjust the path to point at the correct container node
  4. Use Option.SUPPRESS_EXCEPTIONS or defensive reads with defaults for optional fields

Example fix

// before
int n = JsonPath.read(json, "$.items.length()"); // throws if items is null/number
// after
Object items = JsonPath.read(json, "$.items");
int n = (items instanceof List) ? ((List<?>) items).size() : 0;
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = JsonPath.read(json, "$.items");
if (!(v instanceof List || v instanceof Map || v instanceof String)) throw new IllegalStateException("node has no length");

Type guard

boolean hasLength(Object o) { return o instanceof List || o instanceof Map || o instanceof String; }

Try / catch

try {
    return JsonPath.<Integer>read(json, "$.items.length()");
} catch (JsonPathException e) {
    return 0;
}

Prevention

When it happens

Trigger: Evaluating a path with the length() function, e.g. JsonPath.read(json, "$.items.length()"), where $.items is a number, boolean, or null; calling jsonProvider.length(obj) directly on a scalar; paths that resolved to a primitive because the document shape differs from the expected schema.

Common situations: Counting elements of a field that is sometimes null; length() on a field that changed from array to scalar across API versions; computing sizes in transformation pipelines without schema checks.

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