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

146
Views
Diferentes acciones basadas en el valor JSON en Node

Una pregunta desconcertante. Estoy escribiendo una herramienta de línea de comandos basada en nodos que tiene un archivo json de origen, y luego tomará un archivo JSON de cambios (es decir, agregar, actualizar, eliminar, etc.) y básicamente necesito convertir la operación en un nuevo archivo JSON y salida a un archivo nuevo No es tan simple como parece. Sin argumentos de línea de comando, etc., ¿necesitaría tener un campo de directiva en el JSON así?

La aplicación funcionaría así: $ myapp source.json changes.json newfile.json

 { "action": "addRecord", "payload": { "id": "1", "name": "James Hetfield", "guitar": "More Beer" } }, "action": "deleteRecord", "payload": { "id": "3", "name": "Dave Mustaine", "guitar": "Ole Faithful" } }

Mi estructura JSON probablemente también sea incorrecta, pero me pregunto cómo usaría JSON.parse, JSON.stringify para leer el archivo con la biblioteca fs y hacer que las acciones sucedan al identificar action y luego ejecutar una declaración similar a CRUD con payload

Esto es lo que tengo hasta ahora:

 #!/usr/bin/env node // using fs for parsing for small example const fs = require('fs'); // We do not simply require the file due to scaling. // Large file will cause event locking. To handle // even greater files we would use Node streams. const file = './changes.json'; // target filename for output const target = 'output.json'; fs.readFile(file, 'utf8', function(err, data) { if (err) { console.log('Error' + err); return; } let obj = JSON.parse(data, (key, value) => { if (key === 'action') { if (value === 'addSong') { return value = "Song Added"; } if (value === "addPlaylist") { return value = "Playlist added"; } if (value === "deletePlaylist") { return value = "Playlist deleted"; } } return value; }); console.dir(obj); });

fs parece simplemente leer el archivo y recoger el último valor leído. No es bueno. ¿Se pregunta cómo hacer la comparación y posiblemente estructurar el JSON para que la acción suceda y luego agregarla? o transforme el JSON en un nuevo archivo con la actualización. Atascado.

about 4 years ago · Santiago Trujillo
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!