I am trying to export valid json from a mongodb collection I created using node and instagram\'s api. I must be missing something as it seems like it should be super simple.
As mentioned in the comments, parsing the result of mongodump directly probably isn't a good idea. Mongo doesn't give you any guarantee that the elements in the dump will be a valid json (and they're not).
But if you're so inclined to do this you can use event-stream to read newline delimited stream of objects 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 had a stream of valid json objects (with double quotes), the same code would get shortened 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');
}));
Skipped error handling for clarity.
using themongoexport util use --jsonArray
http://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray