As mentioned in the comments, parsing the mongodump result directly is probably not a good idea. Mongo does not give you any guarantee that the elements in the dump will be valid json (which is not the case).
But if you are so inclined to do this, you can use event-stream to read a line feed with line separators and a parser that allows your json to have single quotes instead of double quotes (i.e. hanson ).
Then your code might look something like this:
var es = require('event-stream'), hanson = require('hanson'), in = process.stdin, out = process.stdout; in //read the input stream .pipe(es.split()) //split it on newline .pipe(es.map(function(data, cb) { if (data === '') { //necessary due to the last element produced by split cb(); } else { cb(null, hanson.parse(data)); //parse the line with hanson } })) .pipe(es.writeArray(function (err, array){ //convert resulting objects to array var strArray = JSON.stringify(array); out.write(strArray + '\n'); //write the resulting array to output stream }));
If you have a stream of valid json objects (with double quotes), the same code will be reduced to:
var es = require('event-stream'), hanson = require('hanson'), in = process.stdin, out = process.stdout; in .pipe(es.split()) .pipe(es.parse()) .pipe(es.writeArray(function (err, array){ var strArray = JSON.stringify(array); out.write(strArray + '\n'); }));
Clarification of errors for clarity.
soulcheck
source share