Pymongo/bson: Convert python.cursor.Cursor object to serializable/JSON object

31,710

Solution 1

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"}}]'

Solution 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
Share:
31,710

Related videos on Youtube

Tarun Dugar
Author by

Tarun Dugar

Been working with frontend development for ~5 years. I mostly work with React/React Native. Github: https://github.com/tarun-dugar Medium: https://medium.com/@tarundugar1992 Twitter: https://twitter.com/tarun_red

Updated on November 15, 2020

Comments

  • Tarun Dugar
    Tarun Dugar over 3 years

    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?

    • alecxe
      alecxe almost 9 years
    • Kyle Pittman
      Kyle Pittman almost 9 years
      Convert the cursor to a list of objects: self.response.write(json.dumps(list(exchangedata)))
    • Martin Konecny
      Martin Konecny almost 9 years
      Mongo returns a cursor object instead of an actual list of data. You will need to convert that to a list first as Kyle mentioned.
    • Tarun Dugar
      Tarun Dugar almost 9 years
      Not working with the list wrapper. It sends across an empty data object.
  • hashark
    hashark over 5 years
    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.
  • XChikuX
    XChikuX over 5 years
    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.
  • Evan Thomas
    Evan Thomas over 3 years
    dumps(c.test.test.find(), indent=4) is better.