0

I am making an api to return all documents inside a collection. The code I have right now works fine as long as I don't include the _id (object id) field. If i don't exclude the _id it throws an error

TypeError: ObjectId('') is not JSON serializable

This is my Current Code snippet:

@application.route('/get_all_societies', methods=['GET'])
def get_all_societies():
    result = list(db['Societies'].find({}, {"_id": 0}))
    return jsonify(success=True, data=result)

I want to know the best way to include ObjectId in the result without it throwing an error.

Ninad Gaikwad
  • 4,272
  • 2
  • 13
  • 23
  • https://stackoverflow.com/questions/21411497/flask-jsonify-a-list-of-objects – David Karlsson Aug 23 '18 at 19:51
  • Possible duplicate of [Pymongo/bson: Convert python.cursor.Cursor object to serializable/JSON object](https://stackoverflow.com/questions/30333299/pymongo-bson-convert-python-cursor-cursor-object-to-serializable-json-object) – Oluwafemi Sule Aug 25 '18 at 14:17

0 Answers0