I had written the code to read the excel file into json, but not able to read it as expected. It return the array of each row. Can someone help me read the data and write it to json file correctly. Thanks in advance. Below is 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)
}
}
)
}
)
I want the json output and write to json file as below:
{
"Sheet1": [
{
"Username": "user1",
"password": "password1"
},
{
"Username": "user2",
"password": "password2"
},
{
"Username": "user3",
"password": "password3"
}
]
}
{ "Sheet1": [ { "Username": "user1", "password": "password1", "firstname":"test1", "lastname":"gender", "age":"20", "gender":"M/F", "location":"abc" } ] }