-
-
Save jacurtis/a14725df1505524b125c88a137916178 to your computer and use it in GitHub Desktop.
How to make boto3 responses with datetime.datetime json serializable - boto3 ec2 describe_instances or rds describe_db_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
# The file that does the voodoo magic | |
# Inspired by: | |
# - https://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable | |
# - https://gist.github.com/jeffbrl/67eed588f2d32afcaf3bf779bd91f7a7 | |
import json | |
import datetime | |
def pretty_print(data): | |
print(json.dumps(data, default=__datetime_handler, indent=4)) | |
def __datetime_handler(x): | |
if isinstance(x, datetime.datetime): | |
return x.isoformat() | |
raise TypeError("Unknown Type") |
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
import boto3 | |
import aws_json # import anytime you know you need to parse annoying aws json | |
ec2 = boto3.client('ec2') | |
ec2_res = ec2.describe_instances() | |
aws_json.pretty_print(ec2_res) | |
rds = boto3.client('rds') | |
rds_res = rds.describe_db_instances() | |
aws_json.pretty_print(rds_res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment