Currently working on a quick little project in python and am attempting to encode an object into a JSON string. I've done this several times before without any problem except for now. Usually I just do the following.
def ClassToEncode :
def __init__(self, arg1, arg2, ..., argn) :
self.attr1 = arg1
self.attr2 = arg2
...
self.attrn = argn
...
def toJSON(self) :
return json.dumps(self, default=lambda o: o.__dict__)
But the problem is that one of my class attributes is a datetime.datetime object and I am being thrown the following error
AttributeError: 'datetime.datetime' object has no attribute '__dict__'
Any thoughts or wraparounds that could enable the functionality of including the datetime attribute into the JSON output??
Thanks in advance!
You can use the isoformat()
method on a datetime object to convert it to an ISO-8601-formatted time string, then serialize it as JSON just fine. On the other end, call datetime.datetime.strptime()
on the formatted string to convert it back into a datetime
object:
>>> from datetime import datetime as dt
>>> now = dt.now()
>>> now
datetime.datetime(2014, 9, 4, 3, 19, 44, 214096)
>>> isonow = now.isoformat()
>>> isonow
'2014-09-04T03:19:44.214096'
>>> fmt = "%Y-%m-%dT%H:%M:%S.%f"
>>> newtime = dt.strptime(isonow, fmt)
>>> newtime
datetime.datetime(2014, 9, 4, 3, 19, 44, 214096)