{"record":{"id":"b75ec6f480477a59","repo":"karatelabs/karate","slug":"failed-to-deserialize-json-to-classname-e-getmessage","errorCode":null,"errorMessage":"Failed to deserialize JSON to ${className}: ${e.getMessage()}","messagePattern":"Failed to deserialize JSON to (.+?): (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/common/Json.java","lineNumber":591,"sourceCode":"            }\n            for (Object v : arr) {\n                if (hasCycle(v, seen)) return true;\n            }\n            seen.remove(o);\n        }\n        return false;\n    }\n\n    /**\n     * Deserialize a JSON string into a Java object of the specified class.\n     * Uses json-smart's JSONValue.parse for bean mapping.\n     */\n    public static Object fromJson(String json, String className) {\n        try {\n            Class<?> clazz = Class.forName(className);\n            return JSONValue.parse(json, clazz);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Failed to deserialize JSON to \" + className + \": \" + e.getMessage(), e);\n        }\n    }\n\n}\n\n","sourceCodeStart":573,"sourceCodeEnd":597,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/common/Json.java#L573-L597","documentation":"Json.fromJson(json, className) loads the named class via Class.forName and asks JSONValue to parse the JSON string directly into an instance of that class. If the class cannot be loaded, the JSON is malformed, or the JSON shape does not map to the class, the original exception is caught and rethrown as a RuntimeException with the class name and underlying message.","triggerScenarios":"Calling Json.fromJson(String json, String className) where: className is misspelled or not on the classpath (Class.forName fails); the json string is invalid JSON; the JSON structure does not fit the target class's fields/constructor.","commonSituations":"Typo in a fully-qualified class name; target class moved/renamed between library versions; parsing a response body whose shape changed; passing non-JSON text (e.g. HTML error page) as the json argument.","solutions":["Validate the json string parses (e.g. JSONValue.parse(json)) before deserializing to the typed class.","Verify className is the exact fully-qualified name of a class available on the runtime classpath.","Check the wrapped exception (getCause()) to distinguish ClassNotFoundException from parse/type-mapping failures.","Ensure the target class has a compatible structure (public fields/setters/constructor) for the JSON being parsed."],"exampleFix":"// before\nObject o = Json.fromJson(body, \"com.example.MissingDto\");\n// after\nString cls = \"com.example.MyDto\"; // verify class exists and is on classpath\ntry {\n    Object probe = JSONValue.parse(body); // syntax check first\n} catch (Exception e) {\n    throw new IllegalStateException(\"body is not valid JSON\", e);\n}\nObject o = Json.fromJson(body, cls);","handlingStrategy":"validation","validationCode":"// before calling Json.fromJson\nif (json == null || json.isBlank()) throw new IllegalArgumentException(\"empty json\");\nJSONValue.parse(json); // throws early if not valid JSON\nClass.forName(className); // throws early if class missing","typeGuard":null,"tryCatchPattern":"try {\n    Object o = Json.fromJson(json, className);\n} catch (RuntimeException e) {\n    Throwable cause = e.getCause();\n    // branch on ClassNotFoundException vs parse/mapping failure\n}","preventionTips":["Keep target DTO classes stable and on the compile+runtime classpath.","Parse generically first to sanity-check payloads before typed deserialization.","Store fully-qualified class names as constants, not inline strings."],"tags":["json","deserialization","reflection"],"backgroundTag":"json-unmarshal-failed","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"}