npm i xlsx
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
const xlsx = require('xlsx'); const fs = require('fs'); // Read the JSON file const jsonData = JSON.parse(fs.readFileSync('output.json')); const dataArray = jsonData.users || jsonData; // If dataArray is still not an array, wrap it const finalData = Array.isArray(dataArray) ? dataArray : [dataArray]; // Log the data to verify it's not empty console.log("Data being converted:", finalData.length, "records found"); // Create worksheet from the array const worksheet = xlsx.utils.json_to_sheet(finalData); const workbook = xlsx.utils.book_new(); xlsx.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); xlsx.writeFile(workbook, 'output.xlsx'); console.log('JSON to Excel conversion complete!'); |