I'm trying to consume an ATOM feed of concert data and output it to JSON for a bit nicer consumption.
So far I've been using request to get the data and feedparser to parse through it and it seems to be working as I'd like.
// data
var feed = 'http://mix.chimpfeedr.com/630a0-dcshows';
var wstream = fs.createWriteStream('data.json');
var req = request(feed);
var feedparser = new FeedParser({
addmeta: false
});
req.on('response', function(res) {
var stream = this;
if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
stream.pipe(feedparser)
});
feedparser.on('readable', function() {
var stream = this;
var item;
// ... do some business work to get a `data` object
wstream.write( JSON.stringify(data) + ',' );
});
This writes a file that's literally a concatenated list of these data objects:
{
object1
}, {
object2
}, {
etc
},
This is cool but I'd like this to be wrapped in an array and I'd like the last item to not have the comma after it. I'm sure there are ways I could hack around this but I think I'm missing a core concept of the stream approach and what's actually happening.
So my question is: How do I manipulate the a readable stream (XML) and output an array of valid JSON?