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

后端 未结 3 1256
臣服心动
臣服心动 2021-02-05 06:14

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 r

3条回答
  •  囚心锁ツ
    2021-02-05 06:23

    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)
    

提交回复
热议问题