How to parse XLSX with Node and create JSON with JavaScript?

Sometimes, we want to parse XLSX with Node and create JSON with JavaScript.

In this article, we’ll look at how to parse XLSX with Node and create JSON with JavaScript.

How to parse XLSX with Node and create JSON with JavaScript?

To parse XLSX with Node and create JSON with JavaScript, we use the xlsx package.

To install it, we run

npm i xlsx

Then we use it by writing

const XLSX = require("xlsx");
const workbook = XLSX.readFile("Master.xlsx");
const sheetNameList = workbook.SheetNames;
console.log(XLSX.utils.sheet_to_json(workbook.Sheets[sheetnamelist[0]]));

We call readFile with the path of the file to read.

We get an array of sheet names with workbook.SheetNames.

And we parse the first sheet to JSON with XLSX.utils.sheet_to_json(workbook.Sheets[sheetnamelist[0]]).

Conclusion

To parse XLSX with Node and create JSON with JavaScript, we use the xlsx package.