I'm I getting this TypeError: Object of type 'datetime' is not JSON serializable
error, even though I have a specific serialize function described in my model.
This is my code:
Flask route (rendered by React):
menus.py
@menus_bp.route('/menus', methods=['GET', 'POST'])
def menus():
response_object = {
'status': 'fail',
'message': 'Invalid payload.'
}
try:
user = User.query.filter_by(id=1).first()
if user.menu == []:
return edit_menu()
else:
template = render_template('menus.html')
response_object = {
'status': 'success',
'message': 'success',
'data': [{"restaurant": user.restaurant,
"menu": menu,
"content": template}] # template passed to React
}
# db method
Create_Menu(user=user)
return jsonify(response_object), 200
except (exc.IntegrityError, ValueError):
db.session.rollback()
return jsonify(response_object), 400
methods.py
def Create_Menu(user):
menu = Menu(user=user)
db.session.add(menu)
db.session.commit()
return {"status": True,
"menu": menu}
and finally the Menu model, which has a serialize()
function:
class Menu(db.Model):
__tablename__='menu'
"""
Model for storing menus.
"""
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.DateTime, default=func.now(), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, user):
self.user = user
def serialize(self):
return { 'id' : self.id,
'created': self.created,
'coffees' : [ item.serialize() for item in self.coffees]}
But I'm getting the following traceback:
TypeError: Object of type 'datetime' is not JSON serializable
File "/usr/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/usr/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/usr/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.6/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
return view_func(**req.view_args)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 462, in wrapper
return self.make_response(data, code, headers=headers)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 491, in make_response
resp = self.representations[mediatype](data, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/flask_restful/representations/json.py", line 21, in output_json
dumped = dumps(data, **settings) + "\n"
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/usr/lib/python3.6/json/encoder.py", line 430, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 437, in _iterencode
o = _default(o)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'datetime' is not JSON serializable
It used to work when I was redering templates with Flask at backend, but now with frontend requets, it breaks with the erro above.
what is wrong now? why does not my serialize function work anymore?
Serialize datetime by converting it into String You can convert dateTime value into its String representation and encode it directly, here you don't need to write any encoder. We need to set the default parameter of a json. dump() or json. dumps() to str like this json.
Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.
To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.
See below (using custom JSON encoder)
See http://flask.pocoo.org/snippets/119/ as well
import datetime
import json
class 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"}
As pointed out in the comments in the accepted answer, you can simply use:
json.dumps(my_dict, default=str)
Unless you want to make it more complicated for whatever reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With