Using Flask jsonify with PyMongo Objects
Flask has a handy utility to return json encoded responses from its web controllers. I like to use mongodb for toy projects because its easy to set up and started with. Jsonify doesn't know what to do with PyMongo's ObjectID data types. Here's a drop in replacement I've found useful on a few projects.
1 2 3 4 5 6 7 8
import json def jsonifym(d): "jsonifier that works with mongo objects" return json.dumps(d, default=json_util.default) def my_endpoint(): return jsonifym(my_mongo_db_instance_object)