'How to directly mutate javascript object containing JSON rather than using array

With a source JSON file below, I wish to read in , and apply the delta from a ```changes.JSON file. There is a more elegant way using direction mutation than reading into an array and writing back out. Curious how to do the direct mutation on the object and merge the changes into the source? The changes.json file has "action" and "payload" keys to direction operations on changes.json

source.json

 {
    "users" : [
      {
        "id" : "1",
        "name" : "Dave Mustaine"
      }
    ]
  }

changes.json

{
    "users": [{
        "action": "add",
        "payload": [{
                "id": "2",
                "name": "James Hetfield"
            }
        ]
    }]

becomes this:

{
    "users" : [
      {
        "id" : "1",
        "name" : "Dave Mustaine"
      },
      {
        "id" : "2",
        "name": "James Hetfield"
    ]
}

In my noob implementation I have all the logic written to parse out the action and payload, but currently writing into an array, but I think that is unnecessary and there is a more elegant way to just directly mutate the object and then stringify it and write out the updated json.

Here is what I have so far:

// Stream in source file
    fs.readFile('./' + inputFile, (err, data) => {
    if (err) throw err;
    let input = JSON.parse(data);
        //console.log(input);
    });

    // Stream in changes file
    fs.readFile('./' + changesFile, 'utf-8', (err, jsonString) => {
    if (err)  {
            console.log(err);
        } else {
            try {
                const data = JSON.parse(jsonString);
            
                const array = [];

                Object.entries(data).map(([key, [{ action,  payload }]]) => {
                    switch (key) {
                        case 'users': {
                            if (action === 'add') {
                                console.log("it's an add");
                                array.push([`${key}`, `${payload}`]);
                            }
                        break;
                        }

                     case 'playlists': {
                        if (action === 'add') {
                            console.log("it's an add");
                            array.push([`${key}`, `${payload}`]);
                        }
                            break;
                        }
                    
                        case 'songs': {
                        if (action === 'add') {
                            console.log("it's an add");
                            array.push([`${key}`, `${payload}`]);
                        }
                        break;
                    }
                 }
                });
                
                console.log(array);

            } catch (err) {
                console.log('Error parsing JSON', err);
            }
        }
    }); 

    // after we have merged changes and source we need to write out
    fs.appendFile('./' + outputFile, JSON.stringify(array, null, 2), err => {
        if (err) {
            console.log(err);
        } else {
            console.log('File sucessfully written');
        }
    });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source