You can also do this:
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
print json.dumps(data, default=date_handler)
From here.
Update as per J.F.Sebastian comment
def date_handler(obj):
if hasattr(obj, 'isoformat'):
return obj.isoformat()
else:
raise TypeError
print json.dumps(data, default=date_handler)