JSON serializing Mongodb

后端 未结 5 1462
南笙
南笙 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:17

    I was facing the same issue, I wrote a code that converts document to dictionary. You can use that for reference. Pass the object obtained by find_one() into documentToJson() method and the results of find() into convertDocumentsToJson. There is type in the name Json, instead the code converts to Dict rather than json.

    from bson.json_util import dumps
    
    class UtilService:
    
    def __init__(self):
        pass
    
    @staticmethod
    def pinCodeParser(path):
        location = {}
        f = open(path)
        for line in f:
            words = line.split()
            location[words[1]] = (words[-3],words[-2])
        return location
    
    @staticmethod
    def listHelper(str):
        s = []
        str = str.split(',')
        for e in str:
            s.append(e.replace("[","").replace("]",""))
        return s
    
    @staticmethod
    def parseList(str):
        if ',' in str:
            return UtilService.listHelper(str)
        return str
    
    @staticmethod
    def trimStr(str):
        return str.replace('"','')
    
    @staticmethod
    def documentToJson(document):
        document = eval(dumps(document))
        mp = {}
        for key, value in document.iteritems():
            if "_id" in key:
                mp["id"] = str(value["$oid"])
            else:
                mp[ UtilService.trimStr(key) ] = UtilService.parseList( value )
        return mp
    
    @staticmethod
    def convertDocumentsToJson(documents):
        result = []
        for document in documents:
            result.append(UtilService.documentToJson(document))
        return result
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-05 05:21

    The pymongo documentation you pointed is obsolete. If you're using version 1.7 I recommend updating. With a more recent version you can do this:

    from bson.json_util import dumps
    
    dumps(l)
    

    http://api.mongodb.org/python/current/api/bson/json_util.html

    Side answer: u'name', u'date', u'_id' etc are the names of the fields of the document on the database.

    0 讨论(0)
  • 2020-12-05 05:39
    from bson import json_util
    
    
    
    json.dumps(result,default=json_util.default)
    
    0 讨论(0)
  • 2020-12-05 05:39

    on my situation, this error is due to mongo DB id object in flask all you have to do is convert id if you need it else you can pop it too I'm sharing my solution which I figured out hope this helps someone

    from flask import jsonify
    
    def get_data(self,data):
         data['_id'] = str(data['_id'])
         return data
    
    app =  Flask(__name__)
    
    @app.route('/')
    def apimethod():
         temp = [self.get_data(i) for i in self.critical.find()]
         return jsonify(temp)
    

    also dumps from pymongo don't help alot

    from bson.json_util import dumps,loads
    

    because it is returning a string instead of dict which was expected in my situation to create API and I have to load again if I did dumps.

    0 讨论(0)
提交回复
热议问题