Last active
November 28, 2018 14:25
-
-
Save tiagopotencia/68c5122fd3a1a9c442459bdc2ece2776 to your computer and use it in GitHub Desktop.
Convert Json Array to CSV. Thanks: https://stackoverflow.com/questions/8847766/how-to-convert-json-to-csv-format-and-store-in-a-variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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