0
0
Flaskframework~30 mins

JSON responses with jsonify in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
JSON responses with jsonify
📖 Scenario: You are building a simple web API using Flask. This API will send data back to users in JSON format, which is a common way to share information on the web.
🎯 Goal: Create a Flask app that returns a JSON response using the jsonify function. The response will include a message and a status code.
📋 What You'll Learn
Create a Flask app instance named app
Create a route /hello that returns a JSON response
Use jsonify to send a dictionary with keys message and status
The message value must be "Hello, JSON!"
The status value must be "success"
💡 Why This Matters
🌍 Real World
APIs often send data in JSON format to web or mobile apps. Flask's jsonify makes it easy to send this data correctly.
💼 Career
Knowing how to build JSON APIs with Flask is useful for backend web development jobs and creating services that communicate with frontend apps.
Progress0 / 4 steps
1
Set up Flask app
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use app = Flask(__name__) to create the app instance.

2
Import jsonify
Import jsonify from flask to prepare for sending JSON responses.
Flask
Need a hint?

Add jsonify to the import statement from flask.

3
Create route with JSON response
Create a route /hello using @app.route('/hello'). Define a function hello() that returns a JSON response using jsonify with a dictionary containing message set to "Hello, JSON!" and status set to "success".
Flask
Need a hint?

Use @app.route('/hello') above the function and return jsonify with the correct dictionary.

4
Run the Flask app
Add the code to run the Flask app only if the script is run directly. Use if __name__ == '__main__': and call app.run() inside it.
Flask
Need a hint?

Use the standard Python check if __name__ == '__main__': to run app.run().