I\'m trying to make a response using Flask from a Mongodb collection:
@app.route(\'/stories\', methods = [\'GET\'])
def get_stories():
stories = db.stor
You are encoding twice:
json_docs = [json.dumps(doc, default=json_util.default) for doc in stories]
resp = jsonify(data=json_docs)
Now each entry in json_docs
is a string representing a JSON object.
Remove the json.dumps()
call:
resp = jsonify(data=stories)
or use flask.json.dump()
with a Response()
:
resp = Response(json.dumps({'data': stories}, default=json_util.default),
mimetype='application/json')
This lets you use your json_util.default
handler on the cursor objects still.