{"record":{"id":"8b05918d4144b486","repo":"json-path/JsonPath","slug":"length-operation-can-not-applied-to","errorCode":null,"errorMessage":"length operation can not applied to ","messagePattern":"length operation can not applied to ","errorType":"exception","errorClass":"JsonPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java","lineNumber":250,"sourceCode":"\n        return keys;\n    }\n\n    @Override\n    public int length(final Object obj) {\n        if (isArray(obj)) {\n            return toJsonArray(obj).size();\n        } else if (isMap(obj)) {\n            return toJsonObject(obj).entrySet().size();\n        } else {\n            if (obj instanceof JsonElement) {\n                JsonElement element = toJsonElement(obj);\n                if (element.isJsonPrimitive()) {\n                    return element.toString().length();\n                }\n            }\n        }\n        throw new JsonPathException(\"length operation can not applied to \" + (obj != null ? obj.getClass().getName()\n                                                                                         : \"null\"));\n    }\n\n    @Override\n    public Iterable<?> toIterable(final Object obj) {\n        JsonArray arr = toJsonArray(obj);\n        List<Object> values = new ArrayList<Object>(arr.size());\n        for (Object o : arr) {\n            values.add(unwrap(o));\n        }\n\n        return values;\n    }\n\n    private JsonElement createJsonElement(final Object o) {\n        return gson.toJsonTree(o);\n    }\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java#L232-L268","documentation":"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.","triggerScenarios":"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.","commonSituations":"Gson-backed configurations (Configuration.setJsonProvider(new GsonJsonProvider())) where optional fields are absent → JsonNull; length() on fields that vary in type between API versions.","solutions":["Check the node is non-null and sized before applying length() (element != null && !element.isJsonNull())","Adjust the path to target an array/object/string node","Use SUPPRESS_EXCEPTIONS option or tolerant reads when fields may be missing","Switch computation to an explicit type switch on JsonElement instead of relying on provider.length"],"exampleFix":"// before\nint n = JsonPath.read(json, \"$.items.length()\"); // JsonPathException when items is null\n// after\nJsonElement el = JsonPath.parse(json, gsonConfig).read(\"$.items\");\nint n = (el != null && el.isJsonArray()) ? el.getAsJsonArray().size() : 0;","handlingStrategy":"type-guard","validationCode":"JsonElement el = JsonPath.parse(json, gsonConfig).read(\"$.items\");\nif (el == null || el.isJsonNull()) throw new IllegalStateException(\"$.items is null\");","typeGuard":"boolean hasLength(JsonElement e) { return e != null && !e.isJsonNull() && (e.isJsonArray() || e.isJsonObject() || e.isJsonPrimitive()); }","tryCatchPattern":"try {\n    return JsonPath.<Integer>read(json, \"$.items.length()\");\n} catch (JsonPathException e) {\n    return 0;\n}","preventionTips":["Null-check Gson fields (JsonNull) before length()","Remember JsonNull is a singleton — test with isJsonNull, not == null on nested nodes","Test paths against real payloads with optional fields","Use SUPPRESS_EXCEPTIONS with GsonJsonProvider for tolerant reads"],"tags":["json-path","gson","length","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}