{"record":{"id":"4271daaa51957d1f","repo":"karatelabs/karate","slug":"invalid-json-not-a-json-object-or-array","errorCode":null,"errorMessage":"invalid json: not a JSON object or array","messagePattern":"invalid json: not a JSON object or array","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/common/Json.java","lineNumber":128,"sourceCode":"                throw new IllegalArgumentException(\"input string must not be empty or blank\");\n            }\n            return new Json(JsonPath.parse(parseLenient(s)));\n        } else if (any instanceof List || any instanceof Map) {\n            return new Json(JsonPath.parse(any));\n        } else {\n            String json = JSONValue.toJSONString(any);\n            return new Json(JsonPath.parse(json));\n        }\n    }\n\n    public static Object parseLenient(String json) {\n        if (json == null || json.isBlank()) {\n            throw new RuntimeException(\"invalid json: input is null or blank\");\n        }\n        try {\n            Object result = JSONValue.parseKeepingOrder(json);\n            if (!isMapOrList(result)) {\n                throw new RuntimeException(\"invalid json: not a JSON object or array\");\n            }\n            return result;\n        } catch (RuntimeException e) {\n            throw e;\n        } catch (Exception e) {\n            throw new RuntimeException(\"invalid json: \" + e.getMessage(), e);\n        }\n    }\n\n    public static boolean isMapOrList(Object o) {\n        return o instanceof Map || o instanceof List;\n    }\n\n    public static Object parseStrict(String json) {\n        return parseStrict(json, false);\n    }\n\n    public static Object parseStrict(String json, boolean keepOrder) {","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/common/Json.java#L110-L146","documentation":"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.","triggerScenarios":"Json.parseLenient(\"hello world\"), Json.parseLenient(\"123\"), Json.of(\"null\"), or Json.of(\"\\\"a string\\\"\") — input parses but is not an object/array.","commonSituations":"Feeding log lines, plain text, or scalar values (numbers, quoted strings) where a JSON document was expected.","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"],"exampleFix":"// before\nJson json = Json.of(\"42\");\n// after\nJson json = Json.of(\"{\\\"value\\\": 42}\");","handlingStrategy":"validation","validationCode":"String t = json == null ? null : json.trim();\nboolean looksLikeDocument = t != null && (t.startsWith(\"{\") || t.startsWith(\"[\"));","typeGuard":null,"tryCatchPattern":"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; } }","preventionTips":["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"],"tags":["java","karate","json"],"backgroundTag":"unexpected-response-shape","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}