Convert bson to json in python/pymongo -


i'm trying convert bson data gotten straight pymongo json data. there straight forward way python using pymongo or else?

here's code if 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 , "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 data variable needs turned proper json. there must built in thing this.

for clarity here's when put data python console:

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

objectid not part of json spec...

as discussed in comments above, appears best approach use pymongo's json_util module handle gory details of converting bson json.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -