0

i want to append a object to a array of objects in JSON File with nodeJS

My JSON File

{
  "entreprise": "kem",
  "pesee": [
    {
      "nom": "aaa",
      "prenom": "bbb"
    },
    {
      "nom": "ccc",
      "prenom": "ddd"
    },
    {
      "nom": "eee",
      "prenom": "fff"
    }
  ]
}

i want to add to pesee this object

{
      "nom": "ccc",
      "prenom": "ddd"
    },
Kaneki Em
  • 13
  • 6

4 Answers4

0

You can use fs file system library of node.js to get the file from the system, read it and modify it. Since, the file will read as a text you need to parse the JSON and add your object and then save it back to the file.

var fs = require('fs');
var newObj = {
      "nom": "ccc",
      "prenom": "ddd"
    };
fs.readFile('jsonFile.json', function (err, data) {
    var jsonData = JSON.parse(data);
    jsonData.pesee.push(newObj);

    fs.writeFile("jsonFile.json", JSON.stringify(jsonData));
});
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You can do multiple things:

1.

const myJSON = {
 "entreprise": "kem",
  "pesee": [
    {
      "nom": "aaa",
      "prenom": "bbb"
    },
    {
      "nom": "ccc",
      "prenom": "ddd"
    },
    {
      "nom": "eee",
      "prenom": "fff"
    }
  ]
}

myJSON.pesee.push({
  "nom": "ccc",
  "prenom": "ddd"
})

console.log(myJSON)

2. If you have the JSON in a file, you can read it using this, so the result can be:

fs.readJson('./myJSON')
  .then(data => {
    data.pesee.push({
      "nom": "ccc",
      "prenom": "ddd"
    })

    console.log(data)
  })
  .catch(err => console.log(err))
0

Try this. Once you read in the data, push the new element onto the array. Note that this sample does not check for duplicates, and the example you provided already exists in the array. After this, there will be two sets of nom:ccc, prenom:ddd. This is a synchronous example to keep it simple.

let fs = require('fs');
let json = JSON.parse(fs.readFileSync('data.json'));
json["pesee"].push({"nom":"ccc", "prenom":"ddd"});
fs.writeFileSync('data.json', JSON.stringify(json));
swmcdonnell
  • 1,381
  • 9
  • 22
  • it's work but a question, with this method it's copy all the file and then rewrite all of it, if my JSON file is big (i'm using a json file as a database), will it be really slow? is there any other solution faster?? thnks for the response – Kaneki Em Jun 13 '18 at 06:45
  • Especially for a large file, this is not the fastest solution. Use fs.readFile and fs.writeFile (asynchronous) instead so that reading and writing doesn't block anything else from executing. You might also want to have a look at a database such as MongoDB. It essentially uses JSON but gives you the ability to query and do other database-like tasks on the data. – swmcdonnell Jun 13 '18 at 17:46
-1

You can load the object from your json file in memory and then can use it to write back if you need. Hope it helps you.

var fs = require('fs');
var jsonDataObj = JSON.parse(fs.readFileSync( $filePath, 'utf8'));
var newObj = {
               "nom": "ccc",
               "prenom": "ddd"
              };
jsonDataObj.pesee.push(newObj);