Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Created October 16, 2020 20:16
Show Gist options
  • Save johnmurch/0eb0d447f9177c1fa8bb73a7637034dc to your computer and use it in GitHub Desktop.
Save johnmurch/0eb0d447f9177c1fa8bb73a7637034dc to your computer and use it in GitHub Desktop.
json2csv quick hack
const data = [
{ "name": "John", "time": new Date().toISOString(), "age": "old" },
{
"name": "Jeff", "time": new Date().toISOString(), "age": "older"
}
]
const header = Object.keys(data[0]).map(_ => JSON.stringify(_)).join(';') + '\n'
const outData = data.reduce((acc, row) => {
return acc + Object.values(row).map(_ => JSON.stringify(_)).join(';') + '\n'
}, header)
console.log(outData)
/*
"name";"time";"age"
"John";"2020-10-16T20:15:32.290Z";"old"
"Jeff";"2020-10-16T20:15:32.290Z";"older"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment