{"record":{"id":"496f2c35f9d28ac3","repo":"karatelabs/karate","slug":"jsonpath-needs-two-arguments-object-and-path","errorCode":null,"errorMessage":"jsonPath() needs two arguments: object and path","messagePattern":"jsonPath\\(\\) needs two arguments: object and path","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":278,"sourceCode":"    static JavaInvokable fromJson() {\n        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"fromJson() needs one argument: a JSON string\");\n            }\n            Object arg = args[0];\n            if (arg instanceof String str) {\n                return Json.of(str).value();\n            } else {\n                // Already parsed, just return it\n                return arg;\n            }\n        };\n    }\n\n    static JavaInvokable jsonPath() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"jsonPath() needs two arguments: object and path\");\n            }\n            Object json = args[0];\n            if (json instanceof Node node) {\n                // v1 parity: an XML argument is converted to a Map first, otherwise jayway\n                // sees an opaque scalar and no path can match\n                json = Xml.toObject(node);\n            }\n            String path = args[1].toString();\n            return JsonPath.using(jsonPathConfig).parse(json).read(path);\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable keysOf() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"keysOf() needs one argument\");\n            }","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L260-L296","documentation":"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.","triggerScenarios":"Calling jsonPath(obj) without the path string, jsonPath() with nothing, or accidentally passing the path as the only argument.","commonSituations":"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.","solutions":["Pass both arguments: jsonPath(object, '$.store.book[0].title')","Verify the path variable is a defined non-empty string","For XML, pass the parsed Node — it is auto-converted to a Map before matching"],"exampleFix":"// before\nvar title = karate.jsonPath(payload);\n// after\nvar title = karate.jsonPath(payload, '$.store.book[0].title');","handlingStrategy":"validation","validationCode":"if (obj == null || typeof path !== 'string' || path.length === 0) throw new Error('jsonPath requires an object and a path');","typeGuard":"function canJsonPath(args) { return args.length >= 2 && args[0] != null && typeof args[1] === 'string'; }","tryCatchPattern":"try { val = karate.jsonPath(payload, path); } catch (e) { karate.log('jsonPath failed: ' + e.message); }","preventionTips":["Always pass both the object and the '$...' path","Define path strings as constants to avoid undefined variables","Pass XML Nodes directly — they are auto-converted to maps"],"tags":["argument-count","jsonpath","javascript","karate"],"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"}