I'm trying to write data to a JSON file after that I want to fetch JSON data and parse it and then filter it and write all the table items that share the same tableindex into the same Excel Document or (Excel Table) to print it so, I do this code and it is working but the file is empty by that I mean there is no output it just stringifies the (data) and logs
JSON data is written to the file successfully
in the console but there is no data written in the JSON file
newClient.json
I'm using browserify-fs and I did bundle.js with this javascript code
What I have tried:
async function readFile(fileName) {
console.log({ fileName });
return await Tesseract.recognize(fileName, 'ara', {
logger: m => console.log(m)
});
}
async function parseDataFromTextAndPropertyNames(text, propertyNames) {
console.log({ text, propertyNames });
return propertyNames
.reduce((table, key) =>
Object.assign(table, {
[ key ]: RegExp(`${ key }\\W+(\\w+)`)
.exec(text)?.[1] ?? ''
}), {});
}
async function writeParsedTextDataAsJSON(fileName, table) {
console.log({ table });
const fileSystem = require("browserify-fs")
const data = JSON.stringify({ table })
fileSystem.writeFile("newClient.json", data , err=>{
if(err){
console.log("Error writing file" ,err)
} else {
console.log('JSON data is written to the file successfully')
}
})
return (await new Promise(resolve =>
setTimeout(() => {
console.log({ fileName, json: JSON.stringify({ table }) });
resolve({ success: true });
}, 1500)
));
}
console.log('... running ...');
(async () => {
const { data: { text } } = await readFile('gfhgf.png');
const data = await
parseDataFromTextAndPropertyNames(text, ['نقطة الخدمة', 'رقم العداد']);
const result = await writeParsedTextDataAsJSON('newClient.json', data);
console.log({ result });
})();
this is the first problem the second thing that I have a question how can I filter the data so that they can have a unique index together
{"table":{"نقطة الخدمة":"200135644","رقم العداد":"7038842","tableindex":"1"}}
for example i want all data to have
tableindex
equals to
1
to be in the same Excel document as rows
const url = "newClient.json";
const raw_data = await (await fetch(url)).json();
const prez = raw_data.filter(row => row.tableindex.some(tableindex === "1"));
const rows = tableindex.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
const worksheet = XLSX.utils.json_to_sheet(rows);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");
XLSX.utils.sheet_add_aoa(worksheet, [["watchnumber", "servicepoint"]], { origin: "A1" });
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
XLSX.writeFile(workbook, "newExcel.xlsx");