Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Un comando para agregar datos en otro archivo json

en realidad tengo un archivo json test.json con información como esta:

 { "id": ["1234"] }

Ahora quiero agregar otra identificación 2345 usando discord.js. De hecho, estoy guardando los ID de usuario en el archivo json y ahora quiero crear un comando que introduzca más ID en el archivo test.json.

Por ejemplo: con ese comando puedo agregar otro ID de usuario "2345" para que el archivo json se vea así:

 { "id": ["1234", "2345"] }

Por favor ayúdenme con esto!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hay varias opciones a continuación, todas las cuales deberán trabajarse más para lograr el resultado exacto que necesita, pero esto lo orientará en la dirección correcta.

 const fs = require('fs'); const cName = './path_to_json_from_bot_root_folder.json'; function requireUncached(module) { delete require.cache[require.resolve(module)]; return require(module); } let file; setInterval(() => { file = requireUncached('../../path_to_json_from_this_file.json'); }, 500); function prep(file) { const myJson = JSON.stringify(file); const JsonCut = myJson.slice(0, myJson.length - 1); return JsonCut; } // Option 1 fs.writeFile(cName, 'information to input will completely overwrite the file', { format: 'json', }), (err) => { if (err) throw err; }); // Option 2 fs.writeFile(cName, prep(file) + 'add new information here will have to work through it to make sure that you have all the closing brackets' + '}', { format: 'json', }), (err) => { if (err) throw err; }); // Option 3 const oldInfo = file['subsectionName'] const newInfo = message.content // or whatever method you choose like args if you make it a command fs.writeFile(cName, prep(file) + ${JSON.stringify(file['subsectionName']).trim().replace(`${oldInfo}`, `${newInfo}` + '}', { format: 'json', }), (err) => { if (err) throw err; });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!