Skip to content

Instantly share code, notes, and snippets.

@mbchoa
Created December 16, 2022 04:48
Show Gist options
  • Save mbchoa/8c6e7d5c31374f5cba9d460bf1eaea61 to your computer and use it in GitHub Desktop.
Save mbchoa/8c6e7d5c31374f5cba9d460bf1eaea61 to your computer and use it in GitHub Desktop.
json to csv
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