json-path/JsonPath · error · JsonPathException

length operation can not applied to

Error message

length operation can not applied to 

What it means

JsonOrgJsonProvider.length() implements the JsonProvider length API used by JsonPath's array/object length functions. It only knows how to measure JSONArray/JSONObject and String; for any other object type (or null) it throws JsonPathException with the object's class name. This means the JSON document element the path selected is not a supported length-able type under the org.json provider.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonOrgJsonProvider.java:171

                return new ArrayList<>();
            return jsonObject.keySet();
        } catch (JSONException e) {
            throw new JsonPathException(e);
        }
    }

    @Override
    public int length(Object obj) {
        if (isArray(obj)) {
            return toJsonArray(obj).length();
        } else if (isMap(obj)) {
            return toJsonObject(obj).length();
        } else {
            if (obj instanceof String) {
                return ((String) obj).length();
            }
        }
        throw new JsonPathException("length operation can not applied to " + (obj != null ? obj.getClass().getName()
                : "null"));
    }

    @Override
    public Iterable<?> toIterable(Object obj) {
        try {
            if (isArray(obj)) {
                JSONArray arr = toJsonArray(obj);
                List<Object> values = new ArrayList<Object>(arr.length());
                for (int i = 0; i < arr.length(); i++) {
                    values.add(unwrap(arr.get(i)));
                }
                return values;
            } else {
                JSONObject jsonObject = toJsonObject(obj);
                List<Object> values = new ArrayList<Object>();

                for (int i = 0; i < jsonObject.names().length(); i++) {

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Verify the path target is actually an array, object, or string before applying length(), e.g. read the node and check its type
  2. Change the path so length() is applied to the correct node (e.g. add/adjust an array index or key segment)
  3. If the value can be null, pass a default or use Configuration options (SUPPRESS_EXCEPTIONS / default value) so JsonPath never evaluates length on a missing node
  4. Ensure the configured JsonProvider matches the parsed document type (org.json JSONObject vs another library's types)

Example fix

// before
int n = JsonPath.parse(json).read("$.items.length()"); // items may be a number
// after
Object items = JsonPath.parse(json).read("$.items");
int n = (items instanceof List) ? ((List<?>) items).size()
      : (items instanceof String) ? ((String) items).length() : 0;
Defensive patterns

Strategy: type-guard

Validate before calling

Object node = doc.read(path);
boolean lengthOk = node instanceof List || node instanceof Map || node instanceof String;
if (!lengthOk) throw new IllegalArgumentException("length() not applicable to " + node);

Type guard

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

Try / catch

try {
    int n = JsonPath.parse(json).read("$.items.length()");
} catch (JsonPathException e) {
    // fallback: treat as non-lengthable node
    n = 0;
}

Prevention

When it happens

Trigger: Calling a filter/length function (e.g. path ending in length()) against a node whose runtime type is neither org.json JSONArray/JSONObject nor String — e.g. an Integer or Boolean value returned by the provider's toJsonObject, or null for a missing key.

Common situations: Using length() on a scalar JSON value (number, boolean, null) instead of an array/object/string; switching JSON providers so the object type no longer matches what the provider expects; paths that resolve to null at the tail of the document.

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