Home > Software design >  storing JSON data value in one file using node js / javascript
storing JSON data value in one file using node js / javascript

Time:02-06

let details = [
  {
    value: { tag: [] },
    details: {
      system: "pc",
      systemName: "Dell",
      ram: "4GB",
    },
    Others: {
      OS: "linux",
      harddisk: "125GB",
      Encrypted: "yes",
      password: "abc",
    },
  },
  {
    value: { tag: [] },
    details: {
      system: "pc",
      systemName: "Dell",
      ram: "8GB",
    },
    Others: {
      OS: "Windows",
      harddisk: "125GB",
      Encrypted: "yes",
      password: "xyz",
    },
  },
  {
    value: { tag: [] },
    details: {
      system: "laptop",
      systemName: "Lenovo",
      ram: "4GB",
    },
    Others: {
      OS: "linux",
      harddisk: "256GB",
      Encrypted: "no",
      password: null,
    },
  },
  {
    value: { tag: [] },
    details: {
      system: "pc",
      systemName: "Lenovo",
      ram: "8GB",
    },
    Others: {
      OS: "Ubuntu",
      harddisk: "125GB",
      Encrypted: "yes",
      password: "abc",
    },
  },
  {
    value: { tag: [] },
    details: {
      system: "laptop",
      systemName: "hp",
      ram: "4GB",
    },
    Others: {
      OS: "linux",
      harddisk: "125GB",
      Encrypted: "yes",
      password: "abc",
    },
  },
];

details.map((data) => {
  let folderPath = path.join(__dirname, data.details.systemName);
  fs.mkdir(folderPath, (err) => {
    if (err) throw err;
  });

  fs.writeFile(`${folderPath}/SystemConfig.json`, JSON.stringfy(data.Others, null, 4), (err) => {
    if (err) throw err;
    console.log("Data added Successfully");
  });
});

I am trying to create folder with systemName so I used map function so I can easily created individual Json file in those folder by the help of map function and fs.mkdir I can create folder

now I am trying to store JSON folder by systemConfig.json but the data which is getting store is last only

it only storing the JSON value of the schema of same JSON

as My Expectation is like this

Expectation

Dell/SystemConfig.json

    {
     {
       OS: "linux",
       harddisk: "125GB",
       Encrypted: "yes",
       password: "abc",
     },
     {
       OS: "Windows",
       harddisk: "125GB",
       Encrypted: "yes",
       password: "xyz",
     }
    }

but in reality its storing data in this format

     {
       OS: "Windows",
       harddisk: "125GB",
       Encrypted: "yes",
       password: "xyz",
     }
    }

this follow with same Lenovo folder I am getting same value in Lenovo folder too

with extra } in it how can I store all the Dell details in one file

CodePudding user response:

Here I formatted the data before writing to the files using reduce.

  let details = [
    {
      value: { tag: [] },
      details: {
        system: "pc",
        systemName: "Dell",
        ram: "4GB",
      },
      Others: {
        OS: "linux",
        harddisk: "125GB",
        Encrypted: "yes",
        password: "abc",
      },
    },
    {
      value: { tag: [] },
      details: {
        system: "pc",
        systemName: "Dell",
        ram: "8GB",
      },
      Others: {
        OS: "Windows",
        harddisk: "125GB",
        Encrypted: "yes",
        password: "xyz",
      },
    },
    {
      value: { tag: [] },
      details: {
        system: "laptop",
        systemName: "Lenovo",
        ram: "4GB",
      },
      Others: {
        OS: "linux",
        harddisk: "256GB",
        Encrypted: "no",
        password: null,
      },
    },
    {
      value: { tag: [] },
      details: {
        system: "pc",
        systemName: "Lenovo",
        ram: "8GB",
      },
      Others: {
        OS: "Ubuntu",
        harddisk: "125GB",
        Encrypted: "yes",
        password: "abc",
      },
    },
    {
      value: { tag: [] },
      details: {
        system: "laptop",
        systemName: "hp",
        ram: "4GB",
      },
      Others: {
        OS: "linux",
        harddisk: "125GB",
        Encrypted: "yes",
        password: "abc",
      },
    },
  ];
  
  let formatted = details.reduce((acc,curr) => {
    if(!acc[curr.details.systemName]){
        acc[curr.details.systemName] = [];
    }
    acc[curr.details.systemName].push(curr.Others);
    return acc;
  },{})

console.log(formatted)

After that running a forEach through the keys array of the formatted object

Object.keys(formatted).forEach((data) => {
    let folderPath = path.join(__dirname, data);
    if (!fs.existsSync(folderPath)){ //checking if folder not exists
        fs.mkdir(folderPath, (err) => {
            if (err) throw err;
        });
    }

    fs.writeFile(`${folderPath}/SystemConfig.json`, JSON.stringify(formatted[data], null, 4), (err) => {
      if (err) throw err;
      console.log("Data added Successfully");
    });
  });

You will get data in the format for example in
Dell/SystemConfig.json

[
    {
        "OS": "linux",
        "harddisk": "125GB",
        "Encrypted": "yes",
        "password": "abc"
    },
    {
        "OS": "Windows",
        "harddisk": "125GB",
        "Encrypted": "yes",
        "password": "xyz"
    }
]

which is valid json

CodePudding user response:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

So, if I understood your question correctly, then you will need to move the code that writes to a file outside of the map() callback.

e.g.

let dataToWrite = details.map( data => {
    // do what you need to do here for every element

    return something
})

// write to file here

  •  Tags:  
  • Related