TypeError: Object of type 'datetime' is not JSON serializable (with serialize function) TypeError: Object of type 'datetime' is not JSON serializable (with serialize function) flask flask

TypeError: Object of type 'datetime' is not JSON serializable (with serialize function)


See below (using custom JSON encoder)

See http://flask.pocoo.org/snippets/119/ as well

import datetimeimport jsonclass DateTimeEncoder(json.JSONEncoder):    def default(self, z):        if isinstance(z, datetime.datetime):            return (str(z))        else:            return super().default(z)my_dict = {'date': datetime.datetime.now()}print(json.dumps(my_dict,cls=DateTimeEncoder))

output

{"date": "2019-06-12 15:44:14.978766"}