//change the value in the in-memory object
content.val1 = 42;
//Serialize as JSON and Write it to a file
fs.writeFileSync(filename, JSON.stringify(content));
function save(item, path = './collection.json'){
if (!fs.existsSync(path)) {
fs.writeFile(path, JSON.stringify([item]));
} else {
var data = fs.readFileSync(path, 'utf8');
var list = (data.length) ? JSON.parse(data): [];
if (list instanceof Array) list.push(item)
else list = [item]
fs.writeFileSync(path, JSON.stringify(list));
}
}