I am making a program that reads through an array containing hundreds of links, reads through each, and scrapes some text, then writes it in an output file as json.
I am having trouble formatting the json file, since .map()
returns the json object like this:
{
"id": "wajnh3ivnydeegrr",
"lorem ipsum"
},{
"id": "6yuyz57cmrgo5fbe",
"message": "lorem ipsum"
},
Instead of a json array like this:
[{
"id": "wajnh3ivnydeegrr",
"lorem ipsum"
},{
"id": "6yuyz57cmrgo5fbe",
"message": "lorem ipsum"
}]
How can I push each json object into the array while formatted correctly? (the last trailing comma would have to be excluded, somehow). Really appreciate your help! The full request is below:
app.get('/scrape', function(req, res) {
dataDocument.map(function(item, err) {
request(item, function(err, response, html) {
if (!err) {
//Variables
var message, id, date;
var $ = cheerio.load(html);
var message = $(".messagebody").text().trim();
//Get Id
item = item.split('/');
id = item[4];
//Make json entry
var json = {
"id": id,
"message": message,
"date": date
};
json.id = id;
json.message = message;
string = JSON.stringify(json, null, 4, function(json) {
console.log('Successfully written ' + json.id);
});
fs.appendFile('./output', string + ',', function(){
});
}
});
});
via agomez
No comments:
Post a Comment