Skip to content

Instantly share code, notes, and snippets.

@jeffbrl
Created February 27, 2018 17:28
Show Gist options
  • Save jeffbrl/67eed588f2d32afcaf3bf779bd91f7a7 to your computer and use it in GitHub Desktop.
Save jeffbrl/67eed588f2d32afcaf3bf779bd91f7a7 to your computer and use it in GitHub Desktop.
How to make datetime.datetime json serializable - boto3 ec2 describe_instances
# 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))
@neumj
Copy link

neumj commented Mar 30, 2021

genius. thank you

@nirvanabear
Copy link

Amazing! Thank you!!

@x-eNovi-x
Copy link

so many many thanks

@uc-avec
Copy link

uc-avec commented Jun 11, 2021

Awesome, thanks!

@nitzanwei
Copy link

THANK YOU

@TCBeekley
Copy link

This is amazing!! It saved me a TON of time! Thank you so much!

@jeffbrl
Copy link
Author

jeffbrl commented Aug 19, 2021

Thanks to everyone who dropped a note here expressing that my gist helped them. I think it's amusing that my most appreciated contribution to open source is a small snippet that I recorded primarily for future self. I'm glad people found it useful.

@omersho11
Copy link

Thank you! you have no idea how much time you saved me!

@sdn3098
Copy link

sdn3098 commented Feb 23, 2022

Thanks

@IrmantasMarozas
Copy link

Thank you :)

@mikkelramlov
Copy link

Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment