Answers for "how to update data in json file using javascript"

0

how to update a json file javascript

const fs = require('fs');
const fileName = './file.json';
const file = require(fileName);
    
file.key = "new value";
    
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
  if (err) return console.log(err);
  console.log(JSON.stringify(file));
  console.log('writing to ' + fileName);
});
Posted by: Guest on July-09-2021
0

update data in json using javascript

function setUsername(id, newUsername) {
  for (var i = 0; i < jsonObj.length; i++) {
    if (jsonObj[i].Id === id) {
      jsonObj[i].Username = newUsername;
      return;
    }
  }
}

// Call as
setUsername(3, "Thomas");
Posted by: Guest on January-13-2021

Code answers related to "how to update data in json file using javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language