Export pymongo collection to a JSON file

Viewed 1427

How to export pymongo collection to a JSON file, I have a huge collection which has approximately around 1 GB of data, I need an efficient way to export the data and create a JSON file out of it. I am using the below code, as written in one of the stack overflow answers.

def getJSONFromDB():
    db = GetMongo_client()
    collection = db['collection_name']
    cursor = collection.find({})
    file = open("collection.json", "w")
    file.write('[')
    for document in cursor:
      file.write(json.dumps(document))
      file.write(',')
    file.write(']') 

But it gives me the following error: TypeError: Object of type ObjectId is not JSON serializable

1 Answers

The pymongo documentation you pointed is obsolete. If you're using version 1.7 I recommend updating. With a more recent version you can do this:

from bson.json_util import dumps

dumps(l)

https://pymongo.readthedocs.io/en/stable/api/bson/json_util.html

Side answer: u'name', u'date', u'_id' etc are the names of the fields of the document on the database.

Related