karatelabs/karate · error · RuntimeException
invalid json: not a JSON object or array
Error message
invalid json: not a JSON object or array
What it means
After parsing, parseLenient() checks that the result is a JSON object or array; scalars like `123`, `"text"`, `true`, or `null` are rejected with this RuntimeException. Karate's Json document model only supports Map/List roots.
Solutions
- Wrap the scalar in an object or array, e.g. "{\"value\": 123}"
- Use a plain JSON parser (JSONValue.parse) if scalars are legitimate for your use case
- Check that the file/endpoint actually returns JSON documents, not text
Example fix
// before
Json json = Json.of("42");
// after
Json json = Json.of("{\"value\": 42}"); Defensive patterns
Strategy: validation
Validate before calling
String t = json == null ? null : json.trim();
boolean looksLikeDocument = t != null && (t.startsWith("{") || t.startsWith("[")); Try / catch
try { Object o = Json.parseLenient(s); } catch (RuntimeException e) { if (e.getMessage().contains("not a JSON object or array")) { o = Map.of("value", JSONValue.parse(s)); } else { throw e; } } Prevention
- Check the payload starts with { or [ before parsing
- Avoid passing bare scalars to Json-based APIs
- Confirm the endpoint returns JSON documents, not text or numbers
When it happens
Trigger: Json.parseLenient("hello world"), Json.parseLenient("123"), Json.of("null"), or Json.of("\"a string\"") — input parses but is not an object/array.
Common situations: Feeding log lines, plain text, or scalar values (numbers, quoted strings) where a JSON document was expected.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- invalid json: input is null or blank
- invalid json
- input must not be null
- input string must not be empty or blank
- cannot replace root path $
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/4271daaa51957d1f.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/common/Json.java:128
throw new IllegalArgumentException("input string must not be empty or blank");
}
return new Json(JsonPath.parse(parseLenient(s)));
} else if (any instanceof List || any instanceof Map) {
return new Json(JsonPath.parse(any));
} else {
String json = JSONValue.toJSONString(any);
return new Json(JsonPath.parse(json));
}
}
public static Object parseLenient(String json) {
if (json == null || json.isBlank()) {
throw new RuntimeException("invalid json: input is null or blank");
}
try {
Object result = JSONValue.parseKeepingOrder(json);
if (!isMapOrList(result)) {
throw new RuntimeException("invalid json: not a JSON object or array");
}
return result;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("invalid json: " + e.getMessage(), e);
}
}
public static boolean isMapOrList(Object o) {
return o instanceof Map || o instanceof List;
}
public static Object parseStrict(String json) {
return parseStrict(json, false);
}
public static Object parseStrict(String json, boolean keepOrder) {View on GitHub (pinned to a22eb90246)