{"record":{"id":"08545ee17a603d67","repo":"karatelabs/karate","slug":"invalid-json-e-getmessage","errorCode":null,"errorMessage":"invalid json: ${e.getMessage()}","messagePattern":"invalid json: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/common/Json.java","lineNumber":134,"sourceCode":"            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) {\n        if (json == null || json.isBlank()) {\n            throw new RuntimeException(\"invalid json: input is null or blank\");\n        }\n        try {\n            JSONParser parser = new JSONParser(JSONParser.MODE_RFC4627);\n            if (keepOrder) {","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/common/Json.java#L116-L152","documentation":"parseLenient() wraps any non-runtime parsing exception (e.g. syntax errors from the underlying JSON parser) into a RuntimeException prefixed with 'invalid json: ' and preserves the cause. The embedded e.getMessage() typically names the syntax problem and position.","triggerScenarios":"Json.parseLenient(\"{a:1,}\") or Json.of(truncatedJson) — input has content but fails lenient parsing with an underlying parser exception.","commonSituations":"Truncated API responses, hand-edited JSON with stray commas or unquoted keys beyond lenient tolerance, concatenated JSON fragments.","solutions":["Read e.getMessage()/cause to locate the syntax error and fix the JSON source","Validate the payload with a JSON linter before feeding it in","If the source is an API, log the raw response body to spot truncation/HTML error pages"],"exampleFix":"// before\nJson json = Json.of(rawResponse); // 'invalid json: Unexpected token...'\n// after\nif (!rawResponse.trim().startsWith(\"{\") && !rawResponse.trim().startsWith(\"[\")) { log.error(\"non-JSON response: \" + rawResponse); }\nJson json = Json.of(rawResponse);","handlingStrategy":"try-catch","validationCode":"// pre-validate syntax with a strict parse in a dry run, or lint the string\nif (json != null && json.length() > 0 && (json.startsWith(\"<\") )) throw new IllegalArgumentException(\"response looks like HTML, not JSON\");","typeGuard":null,"tryCatchPattern":"try { Object o = Json.parseLenient(s); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"invalid json:\")) { logger.error(\"parse failed: {} cause={}\", e.getMessage(), e.getCause(), e); throw new IllegalArgumentException(\"bad json payload\", e); } throw e; }","preventionTips":["Always log e.getCause() for parse failures to see the real syntax error","Validate payloads with a JSON linter in CI for fixture files","Capture raw response bodies before parsing to debug truncation"],"tags":["java","karate","json","parse"],"backgroundTag":"json-parse-error","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"}