Simple code that is converting a list of maps to a pipe-separated text
Simple code that is converting a list of maps to a pipe-separated text
Psv.groovy
static String toString(List<Map<String, Object>> data) {
List<String> header = data*.keySet().flatten().unique()*.toString()
List<List<String>> cells = ([header] + (0..data.size() - 1).collect { row ->
header.collect { key -> (data[row][key] as String) ?: '' }
}).transpose().collect { column -> column*.padRight(column*.length().max()) }.transpose()
return cells.collect { row -> "|" + row.join("|") + "|" }.join("\r\n")
}