0

I am unable to deserialize the ObjectId() and datetime() from the MongoDB Json Document.

My MongoDB Json Document will be as below:

result = "[(u'profileDetails', {u'basicDetails': {u'dateOfBirth': datetime.datetime(1992, 2, 1, 0, 0), u'customerCode': u'C037799'}, u'xDirLevel': {u'masterCode': 1}}), (u'_id', ObjectId('58872e99321a0c8633291b3f'))]"

I want to deserialize datetime() and ObjectId() functions.

My Python Code is like below:

from bson import json_util

def deserialize_mongoResult(mongoResult)
   result = json.dumps(mongoResult, default=json_util.default)
   print result

when i print the result it is as below:

"[(u'profileDetails', {u'basicDetails': {u'dateOfBirth': datetime.datetime(1992, 2, 1, 0, 0), u'customerCode': u'C037799'}, u'xDirLevel': {u'masterCode': 1}}), (u'_id', ObjectId('58872e99321a0c8633291b3f'))]"

Why it is not deserialized the object

Sarada Akurathi
  • 1,164
  • 7
  • 28
  • 56
  • what do you mean by deserialize datetime() and ObjectId() functions ? what is your expected output ? – arthur Feb 08 '17 at 10:22
  • i want ObjectId() and datetime() functions should not be there in the result. I have found [json_util](http://api.mongodb.com/python/current/api/bson/json_util.html) will do the purpose, but result is not changed after use this. I found same like question asked in stackoverflow , i asked the same question there and they suggested to open new question [same like question in stackoverflow](http://stackoverflow.com/questions/8409194/unable-to-deserialize-pymongo-objectid-from-json/8409539?noredirect=1#comment71388037_8409539) – Sarada Akurathi Feb 08 '17 at 10:28
  • @arthur, i want ObjectId() value should be changed to `{"_id": {"$oid": "4edebd262ae5e93b41000000"}}` and datetime() function i am not sure, it should be converted to json format – Sarada Akurathi Feb 08 '17 at 10:35
  • ok so I assume you have a document coming from pymongo, and you want it as a JSON file so you want **serialize** it. Am I right ? – arthur Feb 08 '17 at 10:41
  • yes, you are correct arthur – Sarada Akurathi Feb 08 '17 at 10:41
  • ok. the problem is that your `result` variable is not valid json. How did you get your `result` variable ? It's list of tuples and should be list of dicts – arthur Feb 08 '17 at 10:47
  • @arthur, before converting to dictionary, i need to convert those ObjectId() and datetime() then only it is allowing to convert. – Sarada Akurathi Feb 08 '17 at 10:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135167/discussion-between-arthur-and-sarada-akurathi). – arthur Feb 08 '17 at 11:04

1 Answers1

2

Let's say I have a collection User in database test and want to serialize it to JSON:

import pymongo
from bson.json_util import dumps
client = pymongo.MongoClient()
db = client.test
user = db["user"]
# I query the database to get one user : 
res = user.find_one({"country": "us"})
In [66]: res
Out[66]: 
{u'_id': ObjectId('5880fa045fa6a6ffa97a82c7'),
 u'date': datetime.datetime(1992, 2, 1, 0, 0),
 u'name': u'steve',
 u'country': u'us'}
In [63]: dumps(res)
Out[63]: '{"country": "us", "_id": {"$oid": "5880fa045fa6a6ffa97a82c7"}, "name": "steve", "date": {"$date": 696902400000}}'

If you want it in a file:

import json
with open("myfile.json", "w") as f:
    json.dump(dumps(res),f)

Now if you cannot modify your result format, you could still do this :

import datetime
from bson.json_util import dumps
from bson.objectid import ObjectId

#transform string to list    
result = eval(result)
result_dict = dict(result)
dumps(result_dict)
Out[79]: '{"profileDetails": {"basicDetails": {"dateOfBirth": {"$date": 696902400000}, "customerCode": "C037799"}, "xDirLevel": {"masterCode": 1}}, "_id": {"$oid": "58872e99321a0c8633291b3f"}}'
arthur
  • 2,319
  • 1
  • 17
  • 24
  • one more small query, `date` came as `696902400000` how to convert this date value to normal date like `dd-mm-yyyy` or `yyyy-mm-dd` format. – Sarada Akurathi Feb 08 '17 at 12:16
  • 1
    `result_dict["date"] = datetime.datetime.strftime(result_dict["date"] , format="%Y-%m-%d")` – arthur Feb 08 '17 at 12:29