karatelabs/karate · error · RuntimeException
mapWithKey() needs two arguments: list and key name
Error message
mapWithKey() needs two arguments: list and key name
What it means
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.
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.
Example fix
// before def wrapped = mapWithKey(items) // after def wrapped = mapWithKey(items, 'item')
Defensive patterns
Strategy: validation
Validate before calling
def wrapped = list ? mapWithKey(list, 'key') : []
Prevention
- 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.
When it happens
Trigger: karate.mapWithKey() with zero or one argument, e.g. mapWithKey(myList) without the key name, or mapWithKey() with nothing.
Common situations: 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.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- map() needs two arguments: list and function
- range() needs at least two arguments: start and end
- repeat() needs two arguments: count and function
- append() needs at least two arguments
- appendTo() needs at least two arguments: list and item(s)
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/d0e9d4c099dd6d44.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:343
static JavaInvokable map() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("map() needs two arguments: list and function");
}
List<?> list = (List<?>) args[0];
JavaCallable fn = (JavaCallable) args[1];
List<Object> result = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
result.add(fn.call(null, new Object[]{list.get(i), i}));
}
return result;
};
}
static JavaInvokable mapWithKey() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("mapWithKey() needs two arguments: list and key name");
}
Object listArg = args[0];
if (listArg == null) {
return new ArrayList<>();
}
List<?> list = (List<?>) listArg;
String key = args[1].toString();
List<Map<String, Object>> result = new ArrayList<>();
for (Object item : list) {
Map<String, Object> map = new LinkedHashMap<>();
map.put(key, item);
result.add(map);
}
return result;
};
}
@SuppressWarnings("unchecked")View on GitHub (pinned to a22eb90246)