Skip to content

Instantly share code, notes, and snippets.

@michael-lynch
Created September 12, 2022 21:02
Show Gist options
  • Save michael-lynch/d6abb69f29d55b98db39c1cb04b87e64 to your computer and use it in GitHub Desktop.
Save michael-lynch/d6abb69f29d55b98db39c1cb04b87e64 to your computer and use it in GitHub Desktop.
JSON to CSV
// 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