After attempting to convert an excel file into json format using my code, I encountered some difficulties in obtaining the desired output. Instead of a proper conversion, it seems to return an array of each row. Any assistance in reading the data correctly and writing it to a json file would be greatly appreciated. Thank you in advance. Below is the relevant section of my code:
plugins/index.js file
const xlsx = require("node-xlsx").default;
const fs = require("fs");
const path = require("path");
module.exports = (on, config) => {
on("task", {
parseXlsx({ filePath }) {
return new Promise((resolve, reject) => {
try {
const jsonData = xlsx.parse(fs.readFileSync(filePath));
resolve(jsonData);
} catch (e) {
reject(e);
}
});
}
});
}
spec.js file
describe('API', () => {
it('readf', () => {
cy.parseXlsx("/Cypress/cypress/fixtures/data.xlsx").then(
(jsonData) => {
const rowLength = Cypress.$(jsonData[0].data).length
for (let index = 0; index < rowLength; index++) {
console.log(jsonData[index].data)
}
}
)
}
)
My goal is to obtain a json output and write it to a json file structured as follows:
{
"Sheet1": [
{
"Username": "user1",
"password": "password1"
},
{
"Username": "user2",
"password": "password2"
},
{
"Username": "user3",
"password": "password3"
}
]
}