json-path/JsonPath · error · JsonPathException

length operation can not applied to

Error message

length operation can not applied to 

What it means

GsonJsonProvider.length computes the length of Gson JsonElement nodes: arrays/lists by size, objects by key count, and JsonPrimitives via toString().length(). If the element is none of these (JsonNull, JsonObject handled elsewhere fails, or unexpected element kind), a JsonPathException is thrown with the offending class name.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java:250

        return keys;
    }

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

    @Override
    public Iterable<?> toIterable(final Object obj) {
        JsonArray arr = toJsonArray(obj);
        List<Object> values = new ArrayList<Object>(arr.size());
        for (Object o : arr) {
            values.add(unwrap(o));
        }

        return values;
    }

    private JsonElement createJsonElement(final Object o) {
        return gson.toJsonTree(o);
    }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Check the node is non-null and sized before applying length() (element != null && !element.isJsonNull())
  2. Adjust the path to target an array/object/string node
  3. Use SUPPRESS_EXCEPTIONS option or tolerant reads when fields may be missing
  4. Switch computation to an explicit type switch on JsonElement instead of relying on provider.length

Example fix

// before
int n = JsonPath.read(json, "$.items.length()"); // JsonPathException when items is null
// after
JsonElement el = JsonPath.parse(json, gsonConfig).read("$.items");
int n = (el != null && el.isJsonArray()) ? el.getAsJsonArray().size() : 0;
Defensive patterns

Strategy: type-guard

Validate before calling

JsonElement el = JsonPath.parse(json, gsonConfig).read("$.items");
if (el == null || el.isJsonNull()) throw new IllegalStateException("$.items is null");

Type guard

boolean hasLength(JsonElement e) { return e != null && !e.isJsonNull() && (e.isJsonArray() || e.isJsonObject() || e.isJsonPrimitive()); }

Try / catch

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

Prevention

When it happens

Trigger: Evaluating length() on a path resolving to a Gson JsonNull or a non-primitive/ non-array element, e.g. JsonPath.read(json, "$.field.length()") where field is null; calling gsonProvider.length(element) on JsonNull.INSTANCE; applying length() to nested containers the provider does not handle in its branch order.

Common situations: Gson-backed configurations (Configuration.setJsonProvider(new GsonJsonProvider())) where optional fields are absent → JsonNull; length() on fields that vary in type between API versions.

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