Created
September 12, 2022 21:02
-
-
Save michael-lynch/d6abb69f29d55b98db39c1cb04b87e64 to your computer and use it in GitHub Desktop.
JSON to CSV
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
// https://stackoverflow.com/a/31536517/2262604 | |
export const jsonToCsv = json => { | |
const replacer = (key, value) => (value === null ? '' : value); | |
const header = Object.keys(json[0]); | |
const csv = [ | |
header.join(','), | |
...json.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(',')), | |
].join('\r\n'); | |
return csv; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment