New to JSON, NodeJs and so on.
What I am hoping to do is receive the following JSON file data to a NodeJS(and Angular) project modify the received data, then send it back to the JSON file.
JSON file:
{
"groups":{
"groupid": [1,2,3],
"name": ["g1","g2","g3"]
},
"rooms": {
"room1": {
"roomid": 1,
"pGroup": ["g1","g2","g3"]
},
"room2": {
"roomid": 2,
"pGroup": ["g3"]
},
"room3": {
"roomid": 3,
"pGroup": ["g1","g2"]
}
}
}
I can receive and store the data by doing the following:
// Load in groups from groups.json
fs.readFile('./data/groups.json', 'utf8', function(err, data){
if (err) throw err;
groups = [];
let gArray = [];
gArray = JSON.parse(data);
console.log(gArray.groups.name[1]);
for(let i = 0; i < gArray.groups.name.length; i++){
groups.push(gArray.groups.name[i]);
}
});
What I am currently using to store the data:
groups.push(newgroup);
//store room in JSON file
let groupString = JSON.stringify(groups);
fs.writeFile('data/groups.json', groupString, 'utf-8', function(err){
if (err) throw err;
});
I understand that the above puts it into a string and stores the array, but I've tried searching for an alternative where I can just edit the parts of the JSON file that I need, but I can't find an answer anywhere.
any help could be appreciated.
Thanks