Skip to content
Advertisement

read all the files and store in one file using node js / javascript

I have 3 Json file in my project/Folder

file1.json

{
  "id":"01",
  "name":"abc",
  "subject":[
    "subject1":"Maths",
    "subject2":"Science"
  ]
}

File2.json

{
  "id":"01",
  "name":"dummy",
  "Degree":[
    "Graduation":"BCom",
    "Post Graduation":"MBA"
  ]
}

File3.json

{
  "id":"BA01",
  "Address":"India",
  "P Address":[
    "State":"MP",
    "City":"Satna"
  ]
}

I wrote a code where I can read my project/Folder so I can read all the data which is present inside the json file and want to append in my output.json

fs.readdir(
    path.join(process.cwd(), "project/Folder"),
    (err, fileNames) => {
      if (err) throw console.log(err.message);
      // Loop fileNames array
      fileNames.forEach((filename) => {
        // Read file content
        fs.readFile(
          path.join(
            process.cwd(),
            "project/Folder",
            `${filename}`
          ),
          (err, data) => {
            if (err) throw console.log(err.message);
            // Log file content
            const output = JSON.parse(data);
            fs.appendFile(
              path.join(
                process.cwd(),
                "project/Folder",
                `output.json`
              ),
              `[${JSON.stringify(output)},]`,
              (err) => {
                if (err) throw console.log(err.message);
              }
            );
          }
        );
      });
    }
  );

my expected output is like this as I want to append the data which I got from file1, file2, file3 json in output.json

[
   {
      file1.json data
   },
   {
      file2.json data
   },
   {
      file3.json data
   }
]

but in reality I am getting this as an output

[
  {
    file1.josn data
  },
]
[
  {
    file2.josn data
  },
]
[
  {
    file3.josn data
  },
]

I don’t know how can I achieve my expected output like this even I wrote code properly but I think I am missing something, but I don’t know what can someone help me to achieve my expected code?

[
   {
      file1.json data
   },
   {
      file2.json data
   },
   {
      file3.json data
   }
]

Advertisement

Answer

const arr = [];

fs.readdir(path.join(process.cwd(), "project/Folder"), (err, fileNames) => {
  if (err) throw console.log(err.message);
  // Loop fileNames array
  fileNames.forEach((filename) => {
    // Read file content
    fs.readFile(
      path.join(process.cwd(), "project/Folder", `${filename}`),
      (err, data) => {
        if (err) throw console.log(err.message);
        // Log file content
        const output = JSON.parse(data);
        arr.push(output);
        fs.writeFileSync(
          path.join(process.cwd(), "project/Folder", `output.json`),
          JSON.stringify(arr),
          (err) => {
            if (err) throw console.log(err.message);
          }
        );
      }
    );
  });
});

maybe like this, push it to an array and then save it to a new file

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement