Skip to content

Instantly share code, notes, and snippets.

@krmaxwell
Last active September 8, 2017 16:23
Show Gist options
  • Save krmaxwell/98936b0523ca27024d33da93caaf9640 to your computer and use it in GitHub Desktop.
Save krmaxwell/98936b0523ca27024d33da93caaf9640 to your computer and use it in GitHub Desktop.
Dealing with datetime objects when dumping JSON
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