Skip to content

Instantly share code, notes, and snippets.

@tiagopotencia
Last active November 28, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagopotencia/68c5122fd3a1a9c442459bdc2ece2776 to your computer and use it in GitHub Desktop.
Save tiagopotencia/68c5122fd3a1a9c442459bdc2ece2776 to your computer and use it in GitHub Desktop.
var json = []
var fields = Object.keys(json[0])
var replacer = function(key, value) { return value === null ? '' : value }
var csv = json.map(function(row){
return fields.map(function(fieldName){
return JSON.stringify(row[fieldName], replacer)
}).join(',')
})
csv.unshift(fields.join(',')) // add header column
console.log(csv.join('\r\n'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment