Skip to content

Instantly share code, notes, and snippets.

@trisberg
Created July 14, 2015 16:04
Show Gist options
  • Save trisberg/abe55a2fc7c3a5b548f2 to your computer and use it in GitHub Desktop.
Save trisberg/abe55a2fc7c3a5b548f2 to your computer and use it in GitHub Desktop.
Convert Map to CSV line
def result = ""
def delim = ","
def enclose = "\""
payload.each { key, value ->
if (value instanceof String) {
csvValue = enclose + value + enclose
}
else {
csvValue = value
}
if (result.length() > 0) {
result += delim
}
result += csvValue
}
return result
@rodvlopes
Copy link

List of maps to CSV with headers

def delim = ";"
def payload = [[a:1, b:'abc'], [a:2, b:'ced']]
[
  payload[0].keySet().join(delim),
  payload.collect{ it.values().join(delim) }
]
.flatten().join('\n') 

Result

a;b
1;abc
2;ced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment