JSON serializing Mongodb

后端 未结 5 1476
南笙
南笙 2020-12-05 05:10

I am using the python package pymongo to retrieve data from a mongodb database.

>>> r = collection.find()   # returns an object of class \'Cursor\'
         


        
5条回答
  •  难免孤独
    2020-12-05 05:18

    This thread helped me - thank you.

    Wanted to share my final solution to get the JSON back into a JSON/Dictionary Object: (Based on your example)

    from bson.json_util import dumps, loads
    r = collection.find()
    l = list(r) # Converts object to list
    d = dumps(l) # Converts to String
    dict_needed = loads(d[0]) # Serializes String and creates dictionary
    

    Now you have the JSON in a dictionary object and can edit as needed.

提交回复
热议问题