Click here to Skip to main content
15,396,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
  });

  // fake it ...
}
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')
 
    }

})
  // fake it ...
  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();

  /* filter for the Presidents */
  const prez = raw_data.filter(row => row.tableindex.some(tableindex === "1"));

  /* flatten objects */
  const rows = tableindex.map(row => ({
    name: row.name.first + " " + row.name.last,
    birthday: row.bio.birthday
  }));

  /* generate worksheet and workbook */
  const worksheet = XLSX.utils.json_to_sheet(rows);
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");

  /* fix headers */
  XLSX.utils.sheet_add_aoa(worksheet, [["watchnumber", "servicepoint"]], { origin: "A1" });

  /* calculate column width */
  const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
  worksheet["!cols"] = [ { wch: max_width } ];

  /* create an XLSX file and try to save  */
  XLSX.writeFile(workbook, "newExcel.xlsx");
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900