Conversion of JSON to CSV without libraries
const jsonToCSV = (data, columns) => {
// Create CSV string
const makeRow = (columns) => columns.map((c) => (c === undefined ? '' : `${JSON.stringify(c)}`)).join(',') + '\n';
const resolvePath = (data, path) => path.reduce((obj, p) => obj[p], data);
return [
makeRow(columns.map(c => c.title)),
...data.map(d => makeRow(columns.map(c => resolvePath(d, c.path)))),
].join('');
};
const data = [
{ id: 1, user: { name: 'Charlotte' } },
{ id: 2, user: { name: 'Stefan' } },
]
const columns = [
{ title: 'ID', path: ['id'] },
{ title: 'Name', path: ['user', 'name'] },
];
jsonToCSV(data, columns);
"ID","Name"
1,"Charlotte"
2,"Stefan"