Last active
September 8, 2017 16:23
Dealing with datetime objects when dumping JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import datetime | |
class DTEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, datetime.datetime): | |
return obj.isoformat() | |
return json.JSONEncoder.default(self, obj) | |
# assumes data is a dictionary | |
# e.g. data = ig_data.to_dict() | |
def json_output(data): | |
return json.dumps(data, cls=DTEncoder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment