{"record":{"id":"d0e9d4c099dd6d44","repo":"karatelabs/karate","slug":"mapwithkey-needs-two-arguments-list-and-key-name","errorCode":null,"errorMessage":"mapWithKey() needs two arguments: list and key name","messagePattern":"mapWithKey\\(\\) needs two arguments: list and key name","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":343,"sourceCode":"    static JavaInvokable map() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"map() needs two arguments: list and function\");\n            }\n            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                result.add(fn.call(null, new Object[]{list.get(i), i}));\n            }\n            return result;\n        };\n    }\n\n    static JavaInvokable mapWithKey() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"mapWithKey() needs two arguments: list and key name\");\n            }\n            Object listArg = args[0];\n            if (listArg == null) {\n                return new ArrayList<>();\n            }\n            List<?> list = (List<?>) listArg;\n            String key = args[1].toString();\n            List<Map<String, Object>> result = new ArrayList<>();\n            for (Object item : list) {\n                Map<String, Object> map = new LinkedHashMap<>();\n                map.put(key, item);\n                result.add(map);\n            }\n            return result;\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L325-L361","documentation":"mapWithKey() is a Karate JS utility that converts each entry of a List into a Map under a single given key. It throws this error when called with fewer than two arguments: the list and the key name are both required.","triggerScenarios":"karate.mapWithKey() with zero or one argument, e.g. mapWithKey(myList) without the key name, or mapWithKey() with nothing.","commonSituations":"Omitting the key-name string while migrating from map(); misremembering the signature as taking only a list; the key argument is an empty expression that binds no value.","solutions":["Pass both arguments: mapWithKey(myList, 'keyName') produces [{keyName: item}, ...].","Quote the key name — it must be a string, not a bare identifier.","If the list can be null, note the implementation already returns an empty list for null; ensure the key argument is still provided."],"exampleFix":"// before\ndef wrapped = mapWithKey(items)\n// after\ndef wrapped = mapWithKey(items, 'item')","handlingStrategy":"validation","validationCode":"def wrapped = list ? mapWithKey(list, 'key') : []","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the key name as a quoted string as the second argument.","Check the signature when migrating between map() and mapWithKey().","Keep the key name in a variable so the call visibly has two arguments."],"tags":["karate","javascript","argument-count","lists"],"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-19T12:17:13.211Z"}