Reputation: 36337
I'm getting started with flask. I'm trying to return some json from a flask app which I want to generate from a dictionary:
My code looks like:
resp = views.calculate(d)
print type(resp)
print resp
return Flask.json_encoder(**resp)
The output:
<type 'dict'>
{'target1': 'DOES NOT EXIST', 'stage': 0, 'token': u'fsdfsdf', 'target2': 'DOES NOT EXIST', 'text': ''}
and finally I get The error above. How can I fix this?
Upvotes: 1
Views: 4728
Reputation: 1124548
Flask.json_encoder
is the class used to encode JSON with. It does not accept keyword arguments, nor would calling it produce JSON directly.
To produce a response with JSON encoded data, use flask.jsonify()
instead:
from flask import jsonify
return jsonify(**resp)
If all you need is the JSON string, use flask.json.dumps()
here, passing in the dictionary (and not keyword arguments):
from flask import json
return json.dumps(resp)
In both cases the class referenced by Flask.json_encoder
(via the app
instance) would be used to produce the JSON encoding.
Upvotes: 2