Convert xlxs/xlx to JSON in node js

In many scenario we want to convert our xls sheet to json to process the data. Here we are going to do this with the help on one node module named “SheetJs”.

npm install xlsx

Now this is the easy way to convert worksheet to JSON considering that first row of sheet is a header.

const XLSX = require('xlsx')
let workbook = XLSX.readFile("<your_sheet_path>");
let worksheet = workbook.Sheets[workbook.SheetNames[0]]
let sheetJson = XLSX.utils.sheet_to_json(worksheet);
fs.writeFileSync('abcde.json', JSON.stringify(sheetJson))

If we want to give custom header then we can pass it as option.

let sheetJson = XLSX.utils.sheet_to_json(worksheet, {header:["ABCD", "aqw", "dqeq"]});

This module is more powerful. It can be use for many purpose to deal with xls/xls/xlxs.

Leave a Reply