Created
December 16, 2022 04:48
-
-
Save mbchoa/8c6e7d5c31374f5cba9d460bf1eaea61 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
export const jsonToCsv = (json, filename: string) => { | |
const replacer = (key, value) => (value === null ? "" : value); | |
const header = Object.keys(json[0]); | |
return [ | |
header.join(","), | |
...json.map((row) => | |
header | |
.map((fieldName) => JSON.stringify(row[fieldName], replacer)) | |
.join(",") | |
), | |
].join("\r\n"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment