{"record":{"id":"730ce9abc807fe90","repo":"karatelabs/karate","slug":"filterkeys-needs-at-least-two-arguments","errorCode":null,"errorMessage":"filterKeys() needs at least two arguments","messagePattern":"filterKeys\\(\\) needs at least two arguments","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":203,"sourceCode":"            List<?> list = (List<?>) args[0];\n            JavaCallable fn = (JavaCallable) args[1];\n            List<Object> result = new ArrayList<>();\n            for (int i = 0; i < list.size(); i++) {\n                Object item = list.get(i);\n                Object keep = fn.call(null, new Object[]{item, i});\n                if (Boolean.TRUE.equals(keep)) {\n                    result.add(item);\n                }\n            }\n            return result;\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable filterKeys() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"filterKeys() needs at least two arguments\");\n            }\n            Map<String, Object> source = (Map<String, Object>) args[0];\n            Map<String, Object> result = new LinkedHashMap<>();\n\n            if (args[1] instanceof Map) {\n                // filterKeys(source, keysFromMap) - filter by keys present in the map\n                Map<String, Object> keysMap = (Map<String, Object>) args[1];\n                for (String key : keysMap.keySet()) {\n                    if (source.containsKey(key)) {\n                        result.put(key, source.get(key));\n                    }\n                }\n            } else if (args[1] instanceof List) {\n                // filterKeys(source, [key1, key2, ...])\n                List<String> keys = (List<String>) args[1];\n                for (String key : keys) {\n                    if (source.containsKey(key)) {\n                        result.put(key, source.get(key));","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L185-L221","documentation":"Karate's filterKeys() JS utility builds a new map containing only selected keys of a source map (by a keys map, array, or varargs). It requires at least two arguments: the source map plus a key selector; fewer throw this error.","triggerScenarios":"Calling filterKeys(map) with only the map, or filterKeys() with no arguments; passing null as the second argument.","commonSituations":"Stripping a response field but forgetting the key list; dynamic key lists that end up undefined; confusing with filter() which expects a function.","solutions":["Pass the keys to keep: filterKeys(map, ['a','b']) or filterKeys(map, {a:1})","Verify the variable holding the key list is defined and non-empty","Remember the source must be a Map — convert XML via Xml/Json first if needed"],"exampleFix":"// before\nvar trimmed = karate.filterKeys(response);\n// after\nvar trimmed = karate.filterKeys(response, ['id', 'name']);","handlingStrategy":"validation","validationCode":"if (!map || !keys || (Array.isArray(keys) && keys.length === 0)) throw new Error('filterKeys requires a map and key selector');","typeGuard":"function canFilterKeys(args) { return args.length >= 2 && args[0] != null && args[1] != null; }","tryCatchPattern":"try { trimmed = karate.filterKeys(map, keys); } catch (e) { karate.log('filterKeys failed: ' + e.message); }","preventionTips":["Ensure the key list/map variable is defined and non-empty","Accepts a Map, array of keys, or varargs — pick one form consistently","Convert XML to a map before filtering keys"],"tags":["argument-count","map","javascript","karate"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}