I'm fairly new to Python. So I have a REST API based on Flask.So I have a dictionary as listed below :
dict = {'left': 0.17037454, 'right': 0.82339555, '_unknown_': 0.0059609693} I need to add this to my json response object which is like this :
message = { 'status': 200, 'message': 'OK', 'scores': dict } resp = jsonify(message) resp.status_code = 200 print(resp) return resp I'm getting the following error :
....\x.py", line 179, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: 0.027647732 is not JSON serializable Can some one help me with this? Thanks.
dictas a variable name is a bad idea and could potentially be understood by python as the type name, not as the dictionary and it probably cannot serialize native types to json. Just saying, because no one of the other answers explicitly said it.