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

  1. Pass both arguments: mapWithKey(myList, 'keyName') produces [{keyName: item}, ...].
  2. Quote the key name — it must be a string, not a bare identifier.
  3. 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

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


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)