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

153
Visualizações
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 à 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