karatelabs/karate · error · RuntimeException

jsonPath() needs two arguments: object and path

Error message

jsonPath() needs two arguments: object and path

What it means

Karate's jsonPath() JS utility evaluates a Jayway JSON path against an object (XML Nodes are converted to a Map first for v1 parity). It requires two arguments: the object and the path expression; fewer throw this error.

Solutions

  1. Pass both arguments: jsonPath(object, '$.store.book[0].title')
  2. Verify the path variable is a defined non-empty string
  3. For XML, pass the parsed Node — it is auto-converted to a Map before matching

Example fix

// before
var title = karate.jsonPath(payload);
// after
var title = karate.jsonPath(payload, '$.store.book[0].title');
Defensive patterns

Strategy: validation

Validate before calling

if (obj == null || typeof path !== 'string' || path.length === 0) throw new Error('jsonPath requires an object and a path');

Type guard

function canJsonPath(args) { return args.length >= 2 && args[0] != null && typeof args[1] === 'string'; }

Try / catch

try { val = karate.jsonPath(payload, path); } catch (e) { karate.log('jsonPath failed: ' + e.message); }

Prevention

When it happens

Trigger: Calling jsonPath(obj) without the path string, jsonPath() with nothing, or accidentally passing the path as the only argument.

Common situations: Variable holding the path is undefined; splitting the call across lines and dropping an argument; migrating from karate's $-based path syntax and omitting the source object.

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/496f2c35f9d28ac3. Report an issue: GitHub.

Appendix: source

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

    static JavaInvokable fromJson() {
        return args -> {
            if (args.length < 1) {
                throw new RuntimeException("fromJson() needs one argument: a JSON string");
            }
            Object arg = args[0];
            if (arg instanceof String str) {
                return Json.of(str).value();
            } else {
                // Already parsed, just return it
                return arg;
            }
        };
    }

    static JavaInvokable jsonPath() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("jsonPath() needs two arguments: object and path");
            }
            Object json = args[0];
            if (json instanceof Node node) {
                // v1 parity: an XML argument is converted to a Map first, otherwise jayway
                // sees an opaque scalar and no path can match
                json = Xml.toObject(node);
            }
            String path = args[1].toString();
            return JsonPath.using(jsonPathConfig).parse(json).read(path);
        };
    }

    @SuppressWarnings("unchecked")
    static JavaInvokable keysOf() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("keysOf() needs one argument");
            }

View on GitHub (pinned to a22eb90246)