0
0
Flaskframework~30 mins

JSON response formatting in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
JSON Response Formatting in Flask
📖 Scenario: You are building a simple web API using Flask. This API will return user information in JSON format when a client requests it.
🎯 Goal: Create a Flask app that returns a JSON response with user details formatted correctly.
📋 What You'll Learn
Create a dictionary with user data
Set a configuration variable for the HTTP status code
Use Flask's jsonify to format the response
Return the JSON response with the correct status code
💡 Why This Matters
🌍 Real World
APIs often send data in JSON format to web or mobile apps. Knowing how to format JSON responses in Flask is essential for backend development.
💼 Career
Backend developers frequently build REST APIs using Flask or similar frameworks. This skill is fundamental for creating services that communicate with frontend applications.
Progress0 / 4 steps
1
Create user data dictionary
Create a dictionary called user with these exact entries: 'name': 'Alice', 'age': 30, and 'city': 'New York'.
Flask
Need a hint?

Use curly braces to create a dictionary and separate keys and values with colons.

2
Set HTTP status code variable
Create a variable called status_code and set it to 200 to represent a successful HTTP response.
Flask
Need a hint?

Just assign the number 200 to the variable status_code.

3
Import Flask and create app
Import Flask and jsonify from flask. Then create a Flask app instance called app.
Flask
Need a hint?

Use from flask import Flask, jsonify and then app = Flask(__name__).

4
Create route to return JSON response
Create a route /user using @app.route('/user'). Define a function called get_user that returns jsonify(user) and the status_code variable.
Flask
Need a hint?

Use the @app.route decorator and return jsonify(user), status_code.