Convert bson to json in python/pymongo

14,427

As discussed in the comments above, it appears that the best approach is to use PyMongo's json_util module to handle the gory details of converting BSON to JSON.

Share:
14,427
Brian Yeh
Author by

Brian Yeh

Updated on June 14, 2022

Comments

  • Brian Yeh
    Brian Yeh almost 2 years

    I'm trying to convert bson data gotten straight from pymongo into json data. Is there a straight forward way to do this with python using pymongo or something else?

    Here's the code if it helps:

    def email_main(request):
        collection = get_collection("nimbus").fullcontact_email_data
        if request.method == "POST":
            data = collection.save(dict(request.POST.iterlists()))
            response = HttpResponse(data, content_type="application/json")
        elif request.method == "GET":
            getParams = convertQuerydictToDict(request.GET)
            page, itemsPerPage = getPageDataFromDict(getParams)
            getParamsWithNoPageData = createDictWithoutPageData(getParams)
            data = collection.find(getParamsWithNoPageData)[page:page+itemsPerPage+1]
            response = HttpResponse(data, content_type="application/json")
        else:
            response = HttpResponse(status=404)
        return response
    
    
    def convertQuerydictToDict(queryDict):
        return dict(queryDict.iterlists())
    
    
    def getPageDataFromDict(myDict):
        if "page" in myDict and "itemsPerPage" in myDict:
            page, itemsPerPage = myDict["page"], myDict['itemsPerPage']
        elif "page" in myDict:
            page, itemsPerPage = myDict["page"], 10
        else:
            page, itemsPerPage = 0, 10
        return page, itemsPerPage
    
    def createDictWithoutPageData(myDict):
        newDict = deepcopy(myDict)
        newDict.pop("page", None)
        newDict.pop("itemsPerPage", None)
        return newDict
    

    basically that data variable needs to get turned into proper json. There must be some built in thing that does this.

    For clarity here's what I get when I put data into the python console:

    >>> data
    <pymongo.cursor.Cursor object at 0x4dd0f50>
    >>> data[0]
    {u'blah': u'go', u'_id': ObjectId('540e3fd8bb6933764d5650b7')}
    

    ObjectId is not part of the json spec...