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

228
Views
Tome JSON de un archivo, agréguele una clave. Y luego sobrescriba el archivo con la clave en él

Hola, usuarios/desarrolladores de Stack Overflow:

Estoy tratando de tomar JSON de un archivo usando el módulo fs , agregar una clave a la variable que contiene el JSON y luego sobrescribir el archivo. Básicamente haciendo un Sistema de Ahorro de Datos con fs . Lo he intentado de muchas maneras diferentes, pero me encontré con un error al intentar esto.

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 answers
Answer question

0

Está actualizando json después de JSON.stringfy().

Antes del archivo abc.json


 { 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));

Después de abc.json

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