Removing JSON object from JSON file Removing JSON object from JSON file express express

Removing JSON object from JSON file


You can use filter to remove the user you do not want

var fs = require('fs');var removeUser = "test2";var data = fs.readFileSync('results.json');var json = JSON.parse(data);var users = json.users;json.users = users.filter((user) => { return user.username !== removeUser });fs.writeFileSync('results.json', JSON.stringify(json, null, 2));


Your users aren't keyed off of name, they're in a numerically indexed array. You have to use delete users.users[1], or better yet, use .splice().

If you want to delete based on username, you're going to have to loop through.

users.users.forEach((user, index) => {  if (user.username === 'test2') {    users.users.splice(index, 1);  }});

For anything much more complicated, consider a client-side database like TaffyDB.