Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging several JSON files in TypeScript

I am currently tasked with finding the amount of times a specific email has contacted us. The contacts are stored in JSON files and the key should be "email".

The thing is there are potentially infinite JSON files so I would like to merge them in to a single object and iterate to count the email frequency.

So to be clear I need to read in the JSON content. Produce it as a log
consume the message
transform that message into a tally of logs per email used.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My thought process may be wrong but I am thinking I need to merge all JSON files into a single object that I can then iterate over and manipulate if needed. However I believe I am having issues with the synchronicity of it.

I am using fs to read in (I think in this case 100 JSON files) running a forEach and attempting to push each into an array but the array comes back empty. I am sure I am missing something simple but upon reading the documentation for fs I think I just may be missing it.

const fs = require('fs');

let consumed = [];
const fConsume = () => {
  fs.readdir(testFolder, (err, files) => {
    files.forEach(file => {
      let rawData = fs.readFileSync(`${testFolder}/${file}`);
      let readable = JSON.parse(rawData);
      consumed.push(readable);
    });
  
  })
}
fConsume();
console.log(consumed);

For reference this is what each JSON object looks like, and there are several per imported file.

{
      id: 'a7294140-a453-4f3c-91b6-210819e2c43e',
      email: 'ethan.hernandez@microsoft.com',
      message: 'successfully handled skipped operation.'
    },

>Solution :

fs.readdir() is async, so your function returns before it executes the callback. If you want to use synchronous code here, you need to use fs.readdirSync() instead:

const fs = require('fs');

let consumed = [];
const fConsume = () => {
  const files = fs.readdirSync(testFolder)
  files.forEach(file => {
    let rawData = fs.readFileSync(`${testFolder}/${file}`);
    let readable = JSON.parse(rawData);
    consumed.push(readable);
  });  
}
fConsume();
console.log(consumed);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading