22

New to MongoDb and Python (webapp2). So, I was fetching some data from a mongodb database. But I was unable to use json.dumps on the returned data. Here's my code:

exchangedata = db.Stock_Master.find({"Country": "PHILIPPINES"}, {"_id" : 0})        
self.response.write(json.dumps(exchangedata)) 

This throws an error:

TypeError: pymongo.cursor.Cursor object at 0x7fcd51230290 is not JSON serializable

The type of exchangedata is pymongo.cursor.Cursor. How can I convert it into a json object?

doru
  • 9,022
  • 2
  • 33
  • 43
Tarun Dugar
  • 8,921
  • 8
  • 42
  • 79

3 Answers3

35

Use dumps from bson.json_util:

>>> c = pymongo.MongoClient()
>>> c.test.test.count()
5
>>> from bson.json_util import dumps
>>> dumps(c.test.test.find())
'[{"_id": {"$oid": "555cb3a7fa5bd85b81d5a624"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a625"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a626"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a627"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a628"}}]'
Bernie Hackett
  • 8,749
  • 1
  • 27
  • 20
  • 2
    this solution is good only for very small dataset as it dumps all of the objects fetched by the cursor to a single string. With bigger data it doesn't help and you can as well iterate over all of the results and just save them. – hashark Nov 22 '18 at 10:53
  • 1
    bson package is really bad for large data. Use the bsonjs package instead. In most cases it should suffice for you to convert the ObjectId() alone. – XChikuX Nov 28 '18 at 17:35
  • dumps(c.test.test.find(), indent=4) is better. – Evan Knox Thomas Jan 11 '21 at 14:48
2

Simple Solution

 // Use dumps from bson.json_util:
from bson.json_util import dumps

@app.route("/")
def home_page():

    booking = dumps(mongo.db.bookings.find())
    print booking
    return  booking
Shashwat Gupta
  • 5,071
  • 41
  • 33
0

Yes, of course you can use the dumps() in bson.json_util package. But, the dumps() will give you quoted results. To get clear json result you can use the following code.

clean_json = re.compile('ISODate\(("[^"]+")\)').sub('\\1', 
        dumps(my_db._my_collection.find(query), 
                 json_options=CANONICAL_JSON_OPTIONS))
json_obj = json.loads(clean_json)
Siddhartha
  • 340
  • 2
  • 9
Shirantha Madusanka
  • 1,461
  • 1
  • 11
  • 16