Skip to content

Instantly share code, notes, and snippets.

@maumchaves
Created October 13, 2017 02:22
Show Gist options
  • Save maumchaves/08db9f22a3223bddf463bbd17fec8dab to your computer and use it in GitHub Desktop.
Save maumchaves/08db9f22a3223bddf463bbd17fec8dab to your computer and use it in GitHub Desktop.
Read and overwrite a json file in node.js.
const fs = require('fs');
const filePath = 'file/path.json';
const fileObject = JSON.parse(fs.readFileSync(filePath, 'utf8'));
// Do something with file
try {
fs.writeFileSync(filePath, JSON.stringify(fileObject, null, 2), 'utf8');
console.log("The file was saved!");
}
catch(err) {
console.err("An error has ocurred when saving the file.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment