Issue
i have a question how i can delete a something include a json file ?
that's a example
my file name is test.json
this Before i delete auth code
{
"auth": [
{
"test": 944037
},
{
"tester": 261742
}
]
}
i want for example delete a test
after i delete auth
{
"auth": [
{
"tester": 261742
}
]
}
My All database like that
Solution
Your problem is unclear, are you struggling with
- Reading the file?
- Parsing and manipulating the JSON?
- Saving the file?
Anyways, this code might help you:
const fs = require("fs");
fs.readFile("./test.json", (err, data) => {
let json = JSON.parse(data.toString());
// shift() removes the first object in 'auth'. Manipulate json however you wish.
json["auth"].shift();
// Remove second object (as asked by OP)
json["auth"].pop();
fs.writeFile("./test.json", JSON.stringify(json), function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
It first reads the file, parses the contents into JSON. Then manipulates the JSON, and then saves the file, and will console.log("The file was saved!");
if successful.
Answered By - BookOfCooks Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.