Convert a json object to a comma seperated value (CSV) string

Last updated: Wednesday, June 26, 2024

function jsonToCsv(items: Object[]): string {
  if (items.length == 0 || !items) return '';
  const header = Object.keys(items[0]) as readonly (keyof (typeof items)[number])[];

  const headerString = header.join(',');

  // handle null or undefined values here
  const replacer = (_key: unknown, value: unknown) =>
    !!value ? value.toString() : '';

  const rowItems = items.map((row) => {
    return header
      .map((fieldName) => JSON.stringify(row[fieldName], replacer))
      .join(',');
  });

  // join header and body, and break into separate lines
  const csv = [headerString, ...rowItems].join('\r\n');
  return csv;
}