import json
import datetime
from time import mktime


class MyEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return int(mktime(obj.timetuple()))
return json.JSONEncoder.default(self, obj)
print(json.dumps(obj, cls=MyEncoder))
Why does json serialization of datetime objects in python not work out of the box for datetime objects