Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
A command to add data into another json file

actually I've a json file test.json with information like this:

{
"id": ["1234"]
}

Now I want to add another id 2345 using discord.js. I am actually saving user ids in json file and now I want to make a command that will push more ids into the test.json file.

For example: With that command i can add another userid "2345" so that the json file will look like this:

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

Please Help me regarding this!!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are several options below, all of which will have to be worked further to achieve the exact result you need but this will get you pointed in the right direction.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda