Search code examples
jsontypescriptfs

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.

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: '[email protected]',
      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);