Home > Net >  save JSON value in provided folder in Javascript / node js
save JSON value in provided folder in Javascript / node js

Time:02-18

let obj={
 print:{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 },
 hardware:{
   "9058t05":{
     no:"ct-01",
     refrence:"down-tPO-01"
    }
 },
 stack:{
   "345t5fdfve":{
     option1:"prefer first lost",
     failure:"run backup"
    }
 },
 backupdir:{
   "cjksder8982w":{
   files:"config file.json",
   execute:"run after failure"
   }
 }
};


let Array=[{
  filepath:"print"
 },
 {
  filepath:"hardware"
 },
 {
  filepath:"stack"
 },
 {
  filepath:"backupdir"
 },
];

Array.map((file)=>{
//console.log(file.filepath)
Object.keys(obj).forEach((key)=>{
 
 if(key===file.filepath){
// fs.writeFile(path.join(__dirname,file.filepath,"system.json"), JSON.stringify(Object.values(obj)[0], null, 4))

console.log("yes");
 }
})
});

I am trying to fetch key from the obj JSON object and compare it with the Array filepath so I can push the value in created json file

here by using fs I am trying to create folder which I got from the array

fs.writeFile(path.join(__dirname,file.filepath,"system.json"));

by which I created folder and file system.json in it and I am trying to compare the key from the obj json and filepath from the array and trying to put like this

print/system.json

{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 }

hardware/system.json

{
   "9058t05":{
     no:"ct-01",
     refrence:"down-tPO-01"
   }
}

and so on ...

but the problem is when I do this

JSON.stringify(Object.values(obj)[0], null, 4)

I am getting same output in every file

print/system.json

{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 }

hardware/system.json

{
   "2343192u4":{
     id:"01",
     name:"linux file",
     location:"System config"
    },
   "23438ufsdjh8":{
     id:"02",
     name:"windows file",
     location:"System config"
   }
 }

and so on ...

here print hardware stack backupdir always get changed and there are multiple more files as this is system random generated name thats why I have to compare and the key from object and make a directory of this name

how can I push them in different different folder with there respective value

CodePudding user response:

Try changing

fs.writeFileSync(path.join(__dirname,file.filepath,"system.json"),
   JSON.stringify(Object.values(obj)[0], null, 4))

(which only looks at the first property value in obj) to

fs.writeFileSync(path.join(__dirname,file.filepath,"system.json"),
 JSON.stringify(obj[key], null, 4))

which look up the property object in obj using the key value obtained from the Array entry being processed.

The use of forEach in Object.keys(obj).forEach((key)=>{ prevents stopping the search if a matching filepath is found. Another option would be to use a for of loop that breaks out if a file name has been found:

Array.map((file)=>{
  for( key of Object.keys(obj) { 
    if(key===file.filepath) {
      fs.writeFileSync(path.join(__dirname,file.filepath,"system.json"),
        JSON.stringify(obj[key], null, 4)); 
      break;      
    }
  }
});

Also: avoid using the names of global constructors such as Array as variable names - it's a bug waiting to happen :-)

  • Related