Created
February 27, 2018 17:28
-
-
Save jeffbrl/67eed588f2d32afcaf3bf779bd91f7a7 to your computer and use it in GitHub Desktop.
How to make datetime.datetime json serializable - boto3 ec2 describe_instances
This file contains 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
# Adapted from https://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable | |
import datetime | |
import json | |
import boto3 | |
def datetime_handler(x): | |
if isinstance(x, datetime.datetime): | |
return x.isoformat() | |
raise TypeError("Unknown type") | |
client = boto3.client('ec2') | |
data = client.describe_instances() | |
print(json.dumps(data, default=datetime_handler)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you :)