简体   繁体   中英

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
   }
]
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

Honestly, I don't understand all of it. But this line (the second argument of fs.appendFile ):

`[${JSON.stringify(output)},]`,

If you change it to

`${JSON.stringify(output)},`,

Will make the result look like this:

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

Then you can wrap them with brackets [] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM