Replacing a whole block of data in JSON file

Viewed 232

I am looking for a way to replace a bunch of data in a JSON file without replacing another part of it:

{
  "task": [
    {
      "id": 5,
      "title": "dave",
      "description": "test"
    },
    {
      "id": 6,
      "title": "fddsfsd",
      "description": "fsdfsd"
    },
    {
      "id": 7,
      "title": "fddsfssdfsdfd",
      "description": "fsdfsd"
    },
    {
      "id": 8,
      "title": "fddsfssdfsdfd",
      "description": "fsdfsd"
    }
  ],
  "compteur": [
    {
      "id": 8
    }
  ]
}

I manage to get everything that is in between the brackets of "task" in a variable. My current issue is that I need to replace only what's inside the bracket and not affect the other parts of the file.

This is my code for retrieving the data of "tasks":

    function RemoveNode(idToDelete) {
      return jsonData.task.filter(function(emp) {
        if (emp.id == idToDelete) {
          return false;
        }
        return true;
      });
    }
    var newData = RemoveNode(idToDelete);
    arr1 = JSON.stringify(newData, null, 4);
    console.log("arr1", arr1);

The console.log gives me:

arr1 [
    {
        "id": 5,
        "title": "dave",
        "description": "test"
    },
    {
        "id": 6,
        "title": "fddsfsd",
        "description": "fsdfsd"
    },
    {
        "id": 8,
        "title": "fddsfssdfsdfd",
        "description": "fsdfsd"
    }
]

I actually need to replace this in the original JSON File but I have absolutely no idea how to achieve this.

2 Answers

You can use the spread operator, this will override the task data with your new filtered data

const removeNode = (idToDelete) => 
   jsonData.task.filter((emp) => emp.id != idToDelete);


const newData = RemoveNode(idToDelete);
const updatedJSONData = {...jsonData, task: newData};

If your JSON file is not too large, you could consider changing the task array in your JS object (once you've read or imported it into your program) and then re-writing the json file.

JSON file before the program runs:

{
  "task": [
    {
      "id": 5,
      "title": "dave",
      "description": "test"
    },
    {
      "id": 6,
      "title": "fddsfsd",
      "description": "fsdfsd"
    },
    {
      "id": 7,
      "title": "fddsfssdfsdfd",
      "description": "fsdfsd"
    },
    {
      "id": 8,
      "title": "fddsfssdfsdfd",
      "description": "fsdfsd"
    }
  ],
  "compteur": [
    {
      "id": 8
    }
  ]
}

Let's say we want to remove task objects with id=6. The code:

const myFileContents = require('./myFile.json');
const fs = require('fs');

const removeIdFromTasks = (taskList,idToRemove) => {
    return taskList.filter(task => task.id!=idToRemove);
}

const writeJsonFile = (fileName,content) => {
    fs.writeFile(fileName,content,(err) => {
        if(err){
            console.error(`Error in writing json file: ${e.message}`);
        } else {
            console.log(`File written`);
        }
    })
}

myFileContents.task = removeIdFromTasks(myFileContents.task,6);
writeJsonFile(`myFile.json`,JSON.stringify(myFileContents));

The same file after execution:

{
    "task": [
    {
        "id": 5,
        "title": "dave",
        "description": "test"
    },
    {
        "id": 7,
        "title": "fddsfssdfsdfd",
        "description": "fsdfsd"
    },
    {
        "id": 8,
        "title": "fddsfssdfsdfd",
        "description": "fsdfsd"
    }],
    "compteur": [
    {
        "id": 8
    }]
}
Related