How to return a dict as a JSON response from a Flask view?

As of Flask 1.1.0 a view can directly return a Python dict and Flask will call jsonify automatically.

@app.route("/summary")
def summary():
    d = make_summary()
    return d

If your Flask version is less than 1.1.0 or to return a different JSON-serializable object, import and use jsonify.

from flask import jsonify

@app.route("/summary")
def summary():
    d = make_summary()
    return jsonify(d)

Leave a Comment