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) => {
.map((fieldName) => JSON.stringify(row[fieldName], replacer))
// join header and body, and break into separate lines
const csv = [headerString, ...rowItems].join('\r\n');