Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Different actions based upon JSON value in Node

A perplexing question. I am writing a node based command line tool that has a source json file, and then will take a changes JSON file (i.e. add, update, delete, etc) and I need to basically make the operation into a new JSON file and output to a new file. Not as simple as it sounds. With no command line arguments, etc. you would need to have a directive field in the JSON like so?

The app would work like this: $ 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"
   }
}

My JSON structure is probably wrong as well, but wondering how you would use JSON.parse, JSON.stringify to read the file in with the fs library and actually make actions happen when identifying action and then execute a CRUD like statement with payload

Here is what I have so far:

#!/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 seems to just read through the file and pick up the last value read. Not good. Wondering how to do the compare and possibly structure the JSON to make the action happen and then append? or transform the JSON into a new file with the update. Stuck.

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda