See the Extending encoder section from the json package docs https://docs.python.org/2/library/json.html
I have used this method and found it quite effective. I think this is what you are looking for.
import json
class DatetimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%SZ')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)
json.dumps(dict,cls=DatetimeEncoder)