Skip to content

Instantly share code, notes, and snippets.

@postpostscript
Created June 25, 2018 15:17
Show Gist options
  • Save postpostscript/c88b6aa3759918378ec0738e5279b746 to your computer and use it in GitHub Desktop.
Save postpostscript/c88b6aa3759918378ec0738e5279b746 to your computer and use it in GitHub Desktop.
jsonToCsv.js
function jsonToCsv(headers, rows, rowSep="\r\n") {
function escape(value) {
return `"${value.toString().replace('"', '""')}"`
}
function row(values) {
return values.map(escape).join(',').replace(/(,"")+$/, '')
}
return row(headers) + rowSep + rows.map(row => {
return headers.map(header => row[header] || '')
}).map(row).join(rowSep)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment