{"record":{"id":"6d66767f2ebfabf9","repo":"karatelabs/karate","slug":"tocsv-argument-must-be-a-list-of-maps-got-classname","errorCode":null,"errorMessage":"toCsv() argument must be a list of maps, got: {className}","messagePattern":"toCsv\\(\\) argument must be a list of maps, got: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":545,"sourceCode":"            String className = args[1].toString();\n            // Convert to JSON string and deserialize to the target class\n            String jsonString = Json.of(obj).toString();\n            return Json.fromJson(jsonString, className);\n        };\n    }\n\n    /**\n     * Convert a list of maps to CSV string.\n     * Usage: karate.toCsv([{a:1,b:2},{a:3,b:4}]) => \"a,b\\n1,2\\n3,4\\n\"\n     */\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable toCsv() {\n        return args -> {\n            if (args.length == 0 || args[0] == null) {\n                return \"\";\n            }\n            if (!(args[0] instanceof List)) {\n                throw new RuntimeException(\"toCsv() argument must be a list of maps, got: \" + args[0].getClass().getName());\n            }\n            List<Map<String, Object>> list = (List<Map<String, Object>>) args[0];\n            if (list.isEmpty()) {\n                return \"\";\n            }\n            return DataUtils.toCsv(list);\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable toBytes() {\n        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"toBytes() needs one argument: a list of numbers\");\n            }\n            Object arg = args[0];\n            if (arg instanceof byte[]) {\n                return arg; // already bytes","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L527-L563","documentation":"karate.toCsv() converts a list of Maps into CSV text via DataUtils.toCsv. The library throws this error when the first argument is not a List; the concrete class of the offending value is appended to the message. Null input and empty lists are explicitly tolerated (they return an empty string), so only a wrong-typed non-null argument triggers this.","triggerScenarios":"karate.toCsv(singleMap) instead of a list of maps; passing a Java array, a JSON string, or the result of a query that returned an object rather than a list.","commonSituations":"Exporting API response data to CSV where the response is a single JSON object not wrapped in an array; passing an ArrayList-like custom type; forgetting that toCsv expects List<Map<String,Object>> rows.","solutions":["Wrap a single map in a list: karate.toCsv([myMap]).","Ensure each element is a Map (JSON object); convert arrays or other shapes first.","Verify the data source actually returns a list (e.g. response is an array of objects, not an object)."],"exampleFix":"// before\nkarate.toCsv(response) // response is a single object\n// after\nkarate.toCsv([response])","handlingStrategy":"type-guard","validationCode":"if (rows != null && !Array.isArray(rows)) { throw new Error('toCsv() expects a list of maps; got: ' + typeof rows) }\nif (Array.isArray(rows) && rows.some(r => r == null || typeof r !== 'object' || Array.isArray(r))) { throw new Error('toCsv() rows must all be maps') }","typeGuard":"function isListOfMaps(v) { return Array.isArray(v) && v.every(r => r != null && typeof r === 'object' && !Array.isArray(r)) }","tryCatchPattern":"try { var csv = karate.toCsv(rows) } catch (e) { if (('' + e).indexOf('must be a list of maps') >= 0) { csv = karate.toCsv([rows]) } else { throw e } }","preventionTips":["Wrap single objects in an array before calling toCsv","Validate API responses are arrays of objects before CSV export","Remember null and empty list are safe (return empty string) — only wrong non-null types throw"],"tags":["javascript","argument-validation","csv"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}