karatelabs/karate · error · RuntimeException
toJson() needs at least one argument
Error message
toJson() needs at least one argument
What it means
karate.toJson() converts an object to its JSON representation via Json.of(obj), optionally removing null values when the second argument is true. The library throws this error when called with no argument, since there is nothing to serialize. Additional arguments beyond the second are ignored.
Solutions
- Pass the object: karate.toJson(myObj).
- To remove null values, pass true as the second argument: karate.toJson(obj, true).
- Confirm the variable to serialize exists and is in scope.
Example fix
// before var json = karate.toJson() // after var json = karate.toJson(payload)
Defensive patterns
Strategy: type-guard
Validate before calling
if (obj == null) { throw new Error('toJson() requires an object to serialize') } Type guard
function canToJson(v) { return v !== undefined } Try / catch
try { var json = karate.toJson(obj) } catch (e) { if (('' + e).indexOf('needs at least one argument') >= 0) { throw new Error('toJson(): missing object argument') } throw e } Prevention
- Pass the object explicitly as the first argument
- Pass true as second argument to strip nulls rather than post-processing
- Check the variable exists and is in scope before serializing
When it happens
Trigger: karate.toJson() with zero arguments; calling the function reference without arguments; the object variable being dropped during refactoring.
Common situations: Serializing JS objects or Maps for logging or comparison; using toJson(obj, true) to strip nulls before embedding JSON in requests; dynamic call construction losing the argument.
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
- read() needs at least one argument
- sysenv() needs the environment-variable name
- sysprop() needs the property name
- readAsBytes() needs at least one argument
- get() needs at least one argument
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/a31b31ddffbc00a6.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:585
}
List<Object> list = (List<Object>) arg;
byte[] bytes = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);
if (item instanceof Number num) {
bytes[i] = num.byteValue();
} else {
throw new RuntimeException("toBytes() list must contain only numbers, got: " + item.getClass().getName() + " at index " + i);
}
}
return bytes;
};
}
static JavaInvokable toJson() {
return args -> {
if (args.length < 1) {
throw new RuntimeException("toJson() needs at least one argument");
}
Object obj = args[0];
boolean removeNulls = args.length > 1 && Boolean.TRUE.equals(args[1]);
Object result = Json.of(obj).value();
if (removeNulls) {
removeNullValues(result);
}
return result;
};
}
@SuppressWarnings("unchecked")
private static void removeNullValues(Object obj) {
if (obj instanceof Map) {
Map<String, Object> map = (Map<String, Object>) obj;
map.entrySet().removeIf(e -> e.getValue() == null);
map.values().forEach(KarateJsUtils::removeNullValues);
} else if (obj instanceof List) {View on GitHub (pinned to a22eb90246)