Bongskie
Bongskie

Reputation: 53

How to check in NodeJS if json file already got specific data

I'm trying to make a CRUD operations with NodeJS. But i don't know how to check if JSON file already got the specific part and then update only needed object and not overwrite the other data or add the new data if there is no record of it.

this is what i have so far ( right now the insert operation overwrites the whole file and leaves it with only inserted data ) :

JSON :

JSON:

{
    "id": 1,
    "name": "Bill",
},
{
    "id": 2,
    "name": "Steve",
},

Code :

var operation = POST.operation;  // POST request comes with operation = update/insert/delete

        if (operation == 'insert') {
            fs.readFile("data.json", "utf8", function (err) {
                var updateData = {
                    id: POST.id,
                    name: POST.name,
                }
                var newUser = JSON.stringify(updateData);
                fs.writeFile("data.json", newUsers, "utf8");
                console.log(err);
            })
        }
        else if (operation == 'update') {
            fs.readFile("data.json", "utf8", function (err) { 

            })
        }
        else if (operation == 'delete') {
            fs.readFile("data.json", "utf8", function (err) {

            })
        }
        else
            console.log("Operation is not supported");

The most examples that i've found were with Database and Express.js. So they didn' really help much.

Sorry , i'm a newbie.

Upvotes: 2

Views: 2609

Answers (1)

Nicholas Robinson
Nicholas Robinson

Reputation: 1419

So first off, that is not valid JSON (unless this is only part of the file). You will need to wrap that whole list into an array for it to be valid JSON (You can check if your JSON is valid here)

If you go with a structure like this:

[
    {
        "id": 1,
        "name": "Bill",
    },
    {
        "id": 2,
        "name": "Steve",
    },
]

I think the easiest way to check if the ID already exists will be to read the JSON in as an array and check if the ID has already been assigned. Something like this:

var json = require('/path/to/data.json'); // Your object array

// In the if (operation == 'insert') block
var hadId = json.some(function (obj) {
  return obj.id === POST.ID;
});

if (hasId) {
    // Do something with duplicate
}

json.push({
    id: POST.id,
    name: POST.name
});

// Write the whole JSON array back to file 

More on the array.some function

So basically you will be keeping the whole JSON file in memory (in the json array) and when you make a change to the array, you write the whole updated list back to file.

You may run into problems with this if that array gets very large, but at that point I would recommend looking into using a database. Mongo (all be it not the greatest DB in the world) is fairly easy to get setup and to integrate with JavaScript while playing and experimenting.

I hope this helps!

Good luck

Upvotes: 1

Related Questions