karatelabs/karate · error · RuntimeException

map() needs two arguments: list and function

Error message

map() needs two arguments: list and function

What it means

map() is a Karate JS utility that applies a function to each element of a List (the function receives element and index) and returns a new List. It throws this error when called with fewer than two arguments, because both the source list and the transform function are mandatory.

Solutions

  1. Supply both arguments: map(myList, fn) where fn(item, index) returns the transformed value.
  2. Ensure the second argument is actually a function/lamba (e.g. function(x, i){ return x.name }), not a string or value.
  3. Check the list variable is defined; if it may be null, return an empty list instead of calling map().

Example fix

// before
def names = map(users)
// after
def names = map(users, function(u, i){ return u.name })
Defensive patterns

Strategy: validation

Validate before calling

def out = (list && fn) ? map(list, fn) : []

Prevention

When it happens

Trigger: karate.map() with zero or one argument, e.g. map(myList) without the function, or map() entirely; passing null/undefined so only one effective argument is bound.

Common situations: Forgetting the second (function) argument when converting from a for-loop; passing the function in the wrong order; a variable holding the function is undefined so the arity check trips.

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/357a85f21484be69. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:328

            } else if (obj instanceof Map) {
                // Convert to JSON string, lowercase, parse back
                String json = Json.stringifyStrict(obj).toLowerCase();
                return Json.of(json).value();
            } else if (obj instanceof List) {
                String json = Json.stringifyStrict(obj).toLowerCase();
                return Json.of(json).value();
            } else if (obj instanceof Node) {
                String xml = Xml.toString((Node) obj, false).toLowerCase();
                return Xml.toXmlDoc(xml);
            }
            return obj;
        };
    }

    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) {

View on GitHub (pinned to a22eb90246)