{"record":{"id":"d7cfa0ec51bc3514","repo":"json-path/JsonPath","slug":"length-operation-cannot-be-applied-to","errorCode":null,"errorMessage":"length operation cannot be applied to ","messagePattern":"length operation cannot be applied to ","errorType":"exception","errorClass":"JsonPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java","lineNumber":156,"sourceCode":"            return ((Map) obj).keySet();\n        }\n    }\n\n    /**\n     * Get the length of an array or object\n     *\n     * @param obj an array or an object\n     * @return the number of entries in the array or object\n     */\n    public int length(Object obj) {\n        if (isArray(obj)) {\n            return ((List) obj).size();\n        } else if (isMap(obj)){\n            return getPropertyKeys(obj).size();\n        } else if(obj instanceof String){\n            return ((String)obj).length();\n        }\n        throw new JsonPathException(\"length operation cannot be applied to \" + (obj != null ? obj.getClass().getName()\n                : \"null\"));\n    }\n\n    /**\n     * Converts given array to an {@link Iterable}\n     *\n     * @param obj an array\n     * @return an Iterable that iterates over the entries of an array\n     */\n    @SuppressWarnings(\"unchecked\")\n    public Iterable<?> toIterable(Object obj) {\n        if (isArray(obj))\n            return ((Iterable) obj);\n        else\n            throw new JsonPathException(\"Cannot iterate over \" + obj!=null?obj.getClass().getName():\"null\");\n    }\n\n    @Override","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java#L138-L174","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the node the length() function is applied to is an array, object, or string","Null-check / type-check the resolved value before applying length: read the node first and test its type","Adjust the path to point at the correct container node","Use Option.SUPPRESS_EXCEPTIONS or defensive reads with defaults for optional fields"],"exampleFix":"// before\nint n = JsonPath.read(json, \"$.items.length()\"); // throws if items is null/number\n// after\nObject items = JsonPath.read(json, \"$.items\");\nint n = (items instanceof List) ? ((List<?>) items).size() : 0;","handlingStrategy":"type-guard","validationCode":"Object v = JsonPath.read(json, \"$.items\");\nif (!(v instanceof List || v instanceof Map || v instanceof String)) throw new IllegalStateException(\"node has no length\");","typeGuard":"boolean hasLength(Object o) { return o instanceof List || o instanceof Map || o instanceof String; }","tryCatchPattern":"try {\n    return JsonPath.<Integer>read(json, \"$.items.length()\");\n} catch (JsonPathException e) {\n    return 0;\n}","preventionTips":["Apply length() only to array/object/string nodes","Null-check fields that may be absent before counting","Pin path expectations with schema tests","Use SUPPRESS_EXCEPTIONS for tolerant evaluation"],"tags":["json-path","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"}