Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

3.2K
Vistas
save JSON value in provided folder in Javascript / node js

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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.forEach((file)=>{
  for( let 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 :-)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda