To return JSON in Flask, first you need to import the jsonify
function from the flask module. jsonify
takes a Python dictionary and returns a JSON response to the client.
Here's an example code snippet to return a JSON response:
main.py263 chars13 lines
In this example, we define a simple Flask route named /data
which returns a dictionary as a JSON response to the client. The jsonify
function ensures that the response is properly formatted as JSON.
When we run the Flask application and visit the /data
endpoint in our browser, we should see the following JSON response:
56 chars6 lines
gistlibby LogSnag