{"record":{"id":"f80e7bb13bb824e2","repo":"json-path/JsonPath","slug":"length-operation-can-not-applied-to-f80e7b","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/JsonOrgJsonProvider.java","lineNumber":171,"sourceCode":"                return new ArrayList<>();\n            return jsonObject.keySet();\n        } catch (JSONException e) {\n            throw new JsonPathException(e);\n        }\n    }\n\n    @Override\n    public int length(Object obj) {\n        if (isArray(obj)) {\n            return toJsonArray(obj).length();\n        } else if (isMap(obj)) {\n            return toJsonObject(obj).length();\n        } else {\n            if (obj instanceof String) {\n                return ((String) obj).length();\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(Object obj) {\n        try {\n            if (isArray(obj)) {\n                JSONArray arr = toJsonArray(obj);\n                List<Object> values = new ArrayList<Object>(arr.length());\n                for (int i = 0; i < arr.length(); i++) {\n                    values.add(unwrap(arr.get(i)));\n                }\n                return values;\n            } else {\n                JSONObject jsonObject = toJsonObject(obj);\n                List<Object> values = new ArrayList<Object>();\n\n                for (int i = 0; i < jsonObject.names().length(); i++) {","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonOrgJsonProvider.java#L153-L189","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the path target is actually an array, object, or string before applying length(), e.g. read the node and check its type","Change the path so length() is applied to the correct node (e.g. add/adjust an array index or key segment)","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","Ensure the configured JsonProvider matches the parsed document type (org.json JSONObject vs another library's types)"],"exampleFix":"// before\nint n = JsonPath.parse(json).read(\"$.items.length()\"); // items may be a number\n// after\nObject items = JsonPath.parse(json).read(\"$.items\");\nint n = (items instanceof List) ? ((List<?>) items).size()\n      : (items instanceof String) ? ((String) items).length() : 0;","handlingStrategy":"type-guard","validationCode":"Object node = doc.read(path);\nboolean lengthOk = node instanceof List || node instanceof Map || node instanceof String;\nif (!lengthOk) throw new IllegalArgumentException(\"length() not applicable to \" + node);","typeGuard":"static boolean isLengthable(Object o) {\n    return o instanceof List || o instanceof Map || o instanceof String;\n}","tryCatchPattern":"try {\n    int n = JsonPath.parse(json).read(\"$.items.length()\");\n} catch (JsonPathException e) {\n    // fallback: treat as non-lengthable node\n    n = 0;\n}","preventionTips":["Only use length() on array/object/string nodes","Inspect the node type with a plain read before applying length functions","Use Configuration with SUPPRESS_EXCEPTIONS or defaults for potentially missing nodes","Keep the JsonProvider consistent with the parsed document type"],"tags":["json","jsonpath","unsupported-type"],"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"}