karatelabs/karate · error · RuntimeException
fromJson() needs one argument: a JSON string
Error message
fromJson() needs one argument: a JSON string
What it means
Karate's fromJson() JS utility parses a JSON string into a Java object graph. It requires one string argument; calling it with no arguments throws this error. Non-string arguments are tolerated (returned as already-parsed values), so only the missing-argument case triggers this.
Solutions
- Pass the JSON string: fromJson('{"a":1}')
- Check the variable feeding fromJson is actually defined and a string
- If the value is already an object, fromJson can be dropped entirely
Example fix
// before
var obj = karate.fromJson();
// after
var obj = karate.fromJson('{"a":1}'); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof json !== 'string') throw new Error('fromJson expects a JSON string'); Type guard
function canParseJson(v) { return typeof v === 'string' && v.trim().length > 0; } Try / catch
try { obj = karate.fromJson(str); } catch (e) { karate.log('fromJson failed: ' + e.message); } Prevention
- Check the source variable is a defined string before parsing
- Skip fromJson if the value is already an object (it passes through)
- Log the payload when a previous step might have left it undefined
When it happens
Trigger: Calling fromJson() with zero arguments, e.g. fromJson(), or passing undefined where a JSON string was expected.
Common situations: Variable holding the JSON string is undefined/null due to a failed earlier step; forgetting the argument when converting from Json.parse style code.
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
- eval() needs one argument
- expect() needs at least one argument
- remove() needs two arguments: variable name and path
- 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/3b8890962c8b5620.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:263
if (collection instanceof List<?> list) {
for (int i = 0; i < list.size(); i++) {
fn.call(null, new Object[]{list.get(i), i});
}
} else if (collection instanceof Map) {
Map<String, Object> map = (Map<String, Object>) collection;
int i = 0;
for (Map.Entry<String, Object> entry : map.entrySet()) {
fn.call(null, new Object[]{entry.getKey(), entry.getValue(), i++});
}
}
return null;
};
}
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) {View on GitHub (pinned to a22eb90246)