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

232
Visualizações
Take JSON from a file, add a key to it. And then overwrite the file with the key in it

Hello Stack Overflow Users/Developers,

I'm trying to take JSON from a file using the fs module, add a key to the variable that holds the JSON, and then overwrite the file. Basically making a Data Saving System with fs. I've tried many different ways but I came across an error when trying this.

SyntaxError: Unexpected End of JSON input

var UserTable = JSON.stringify(require('./data/Data'))
    UserTable[`User_${id[key]}`] = value;

    setTimeout(() => {
      console.log(UserTable);
    }, 1000);

    fs.writeFileSync(__dirname + '/data/Data.json', UserTable);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your are updating json after JSON.stringfy().

Before abc.json file


{
   name : "Tom"
}

const appjson = require("./abc.json");
var fs = require('fs');

id = [1, 2, 3, 4] // json or array
key = 3 //index or key
value = "some_value" // data to set

appjson[`User_${id[key]}`] = value;

fs.writeFileSync(__dirname + '/abc.json', JSON.stringify(appjson));

After abc.json

{
    "name": "Tom",
    "User_4": "some_value"
}
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