I am working with an external file called file.json which contains the following values:
{
"number": "value"
}
My goal is to run a function that will append new data to the existing file instead of overwriting it. For instance, after running the function, I would like the file to look like this:
{
"number": "value"
},
{
"number2": "newValue"
}
Currently, my attempts to achieve this using .push() have resulted in 'undefined'. Can anyone suggest how I can modify my code to successfully append data to the file?
Here is the code snippet I am currently using:
var urlList = require('./urlList.json');
app.get('/hello', function(req, res){
var cat = 5;
catNumber = "number" + cat;
url = urlList[catNumber];
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var number;
var json = { };
$('.content').filter(function(){
var data = $(this);
title = data.children().first().text().trim();
json.number = url;
})
}
fs.writeFile('file.json', JSON.stringify(json, null, 4), function(err){
console.log('File successfully written!');
})