Categories
Python Answers

How to return a JSON response from a Python Flask view?

Spread the love

Sometimes, we want to return a JSON response from a Python Flask view.

In this article, we’ll look at how to return a JSON response from a Python Flask view.

How to return a JSON response from a Python Flask view?

To return a JSON response from a Python Flask view, we an return a dictionary directly as the response.

For instance, we write

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

to get the dict d from the make_summary function and then return its contents as the JSON response in the /summary view.

Conclusion

To return a JSON response from a Python Flask view, we an return a dictionary directly as the response.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *