Skip to content

Instantly share code, notes, and snippets.

@meleu
Last active October 9, 2020 16:12
Show Gist options
  • Save meleu/82d8fc7499fa89da0d8b9f35370ff4a5 to your computer and use it in GitHub Desktop.
Save meleu/82d8fc7499fa89da0d8b9f35370ff4a5 to your computer and use it in GitHub Desktop.
write and parse json files
function writeJsonFile(path, obj) {
if (!(typeof obj === 'object' && obj !== null)) {
return false;
}
try {
fs.writeFileSync(path, JSON.stringify(obj, null, 2));
} catch (err) {
console.error(`---[ERRO - ${new Date()}]\n${err}`);
return false;
}
return true;
}
/**
* Retorna o conteúdo do arquivo JSON já transformado em um objeto.
* @param {String} path
*/
function parseJsonFile(path) {
try {
return JSON.parse(fs.readFileSync(path, 'utf8'));
} catch (err) {
console.error(`---[ERRO - ${new Date()}]\n${err}`);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment