{"id":792,"date":"2014-11-03T18:18:53","date_gmt":"2014-11-03T18:18:53","guid":{"rendered":"http:\/\/shivdev.com\/blog\/?p=792"},"modified":"2014-11-03T18:18:53","modified_gmt":"2014-11-03T18:18:53","slug":"python-typeerror-datetime-datetime-not-json-serializable-while-using-mongo","status":"publish","type":"post","link":"http:\/\/shivdev.com\/blog\/2014\/11\/03\/python-typeerror-datetime-datetime-not-json-serializable-while-using-mongo\/","title":{"rendered":"Python TypeError: datetime.datetime not JSON serializable while using Mongo"},"content":{"rendered":"<p>This post from<a href=\"http:\/\/stackoverflow.com\/questions\/11875770\/how-to-overcome-datetime-datetime-not-json-serializable-in-python\"> stackoverflow.com<\/a> explains it.<\/p>\n<p>Basically, you will need to use the <strong>default=json_util.default<\/strong> argument as defined in the <a href=\"http:\/\/api.mongodb.org\/python\/current\/api\/bson\/json_util.html\">PyMongo Docs<\/a><\/p>\n<p>So, while converting a date object in a dictionary to JSON<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom bson import json_util\r\nimport json\r\njson_str = json.dumps(anObject, default=json_util.default)\r\nprint json_str\r\n<\/pre>\n<p>And while reading back the JSON string into a dictionary<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom bson import json_util\r\nimport json\r\njson.loads(json_str, object_hook=json_util.object_hook)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post from stackoverflow.com explains it. Basically, you will need to use the default=json_util.default argument as defined in the PyMongo Docs So, while converting a date object in a dictionary to JSON And while reading back the JSON string into a dictionary<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/792"}],"collection":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/comments?post=792"}],"version-history":[{"count":3,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":795,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions\/795"}],"wp:attachment":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/media?parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/categories?post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/tags?post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}