I am trying to export JSON data to csv / excel by using exceljs library. My data has nested objects and I want to trasnform nested objects to columns.

I have this JSON data
[ { "name": "Raghav", "grade": "I", "marks": [ { "subject": "Maths", "grade": "A1" }, { "subject": "Science", "marks": "B" } ] }, { "name": "Isha", "grade": "II", "marks": [ { "subject": "Maths", "grade": "B1" }, { "subject": "Science", "marks": "A" } ] } ]

Here’s the code to export JSON data to csv

const ExcelJS = require('exceljs');

// excel export start
let workbook = new ExcelJS.Workbook()
let worksheet = workbook.addWorksheet('marks')
worksheet.columns = [

    { header: 'Name', key: 'name' },
    { header: 'Surname', key: 'surname' },
    { header: 'Marks', key: 'marks' },
]

worksheet.getRow(1).font = { bold: true }

async function exportToExcel() {

    console.log('exportToExcel called');
    let queryExport = query(collectionGroup(db, "2023-24"),
        where('year', '==', '2023-24'),
        where('section', '==', 'SSSV GSEB Primary (EM)'),
        where('grade', '==', '8'),
        where('division', '==', 'B'),
        where('exam', '==', 'FA01'),
        orderBy('roll', "asc"),
        limit(2)
    );

// getting data from Firestore
    getDocs(queryExport)
        .then((snapshot) => {
            snapshot.forEach((doc, index) => {
                const rowIndex = index + 2
                let e = doc.data()
                console.log(e);
                let newEntry = doc.data();
                newEntry.name = e.name;
                newEntry.surname = e.surname;

                worksheet.columns.forEach(column => {
                    worksheet.addRow({
                        ...e,
                    })
                })
            });
            workbook.xlsx.writeBuffer()
                .then(buffer => FileSaver.saveAs(new Blob([buffer]), `pcs_bkp_${Date.now()}.xlsx`))
                .catch(err => console.log('Error writing excel export', err))
        });
}

Above code is working properly and I am getting csv file as shown below.

result achieved

But I want to export my nested array trasformed and expected result should like below.

expected result

Please help me to sort it out.