简体   繁体   中英

NodeJS: Pulling data from JSON,modifying and Sending object back to the same JSON file

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

In order to edit the contents of a file you need to actually write to the file

You can always just edit the properties of the json and keep the JavaScript object in memory ok the nodeJS application and if it's a server just send The memory -stored object to the client directly, do it looks like the file was updated when it really wasn't, but then when the server closes so the changes that weren't written to disk wouldn't be saved

If you want to save your progress, you have to write to the disk (or some kind of database etc)

EDIT

but there is still a way to do what you want, it's actually he a lot of databases work, but for json it might be a bit more complex

Basically the is a function in node is that can read only specific bytes of a file at a time

https://stackoverflow.com/a/23720178/2016831

But the real way is to use fs.open then fs.write(look it up) to read/write to specific byte indexes of the file

The problem is how do you know what indexes of the file to write to, which is a whole science of how databases work etc usually binary is easier because it's split into different chunks and each chunk has some header bytes that refer to the byte index of the chunks before and after it etc then you need to read one root chunk which tells you what Uther chunks to read read etc until u find the part you why to edit, then after you do so you need to update some of the other chunks, or sometimes not necessarily etc look up how to make a database from scratch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM