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. I've read other posts and mongo's documentation, specifically about mongoexport.
My end goal is to build a d3 map. I used the basic mongoexport command from the documentation and it returned a json file in the following format:
{'name':'dan'}
{'name':'emma'}
valid json would be:
[{'name':'dan'},
{'name':'emma'}]
I know there are workarounds to this, even one as simple as finding '$' in sublime text which would go to the end of every line, and then could just add a coma. It would just be great to know the proper way technically of doing this. One post suggested using JSON.parse, so I tried this using fs.readFile but it returns the error:
undefined:2
{ "attribution" : null, "tags" : [], "location" : { "latitude" : 48.857141667,
^
SyntaxError: Unexpected token {.
I simply just need to export the entire mongo collection into a json file that's valid (would successfully pass http://jsonlint.com/ test).
Any help would really be appreciated.
So, to export data from the MongoDB database, MongoDB provides a command-line tool known as mongoexport. Using this tool you can exports data of a collection in JSON or CSV(comma-separated value) format. Moreover, we can also use features like limit and sort on a collection while exporting the data.
using themongoexport util use --jsonArray
http://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With