Challenge - 5 Problems
JSON Data Mastery in Flask
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask route when sending JSON data?
Consider this Flask route that receives JSON data and returns a value from it. What will be the response if the client sends {"name": "Alice", "age": 30} as JSON?
Flask
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/user', methods=['POST']) def user(): data = request.get_json() return jsonify({'greeting': f"Hello, {data['name']}!"})
Attempts:
2 left
💡 Hint
Look at how the JSON data is accessed by key 'name' in the dictionary.
✗ Incorrect
The route uses request.get_json() to parse the JSON body into a dictionary. Then it accesses data['name'], which is 'Alice', and returns it in the greeting.
📝 Syntax
intermediate1:30remaining
Which option correctly accesses nested JSON data in Flask?
Given JSON data {"user": {"id": 5, "info": {"email": "a@example.com"}}}, which code correctly extracts the email inside a Flask route?
Flask
data = request.get_json() email = ???
Attempts:
2 left
💡 Hint
Follow the nested keys step by step from the JSON structure.
✗ Incorrect
The email is nested inside 'user' then 'info', so you must access data['user']['info']['email']. Other options miss levels or keys.
🔧 Debug
advanced2:00remaining
Why does this Flask route raise an error when accessing JSON data?
This route tries to get a JSON key but raises an error. What is the cause?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/data', methods=['POST']) def data(): data = request.json return data['key']
Attempts:
2 left
💡 Hint
Check the request headers and how Flask parses JSON automatically.
✗ Incorrect
If the client does not send Content-Type: application/json, request.json returns None, so accessing data['key'] causes a TypeError. The error is due to request.json being None.
❓ state_output
advanced2:00remaining
What is the output of this Flask route when JSON data is missing a key?
Given this route, what happens if the client sends JSON {"name": "Bob"} but the code tries to access 'age'?
Flask
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/info', methods=['POST']) def info(): data = request.get_json() age = data['age'] return jsonify({'age': age})
Attempts:
2 left
💡 Hint
What happens when you access a dictionary key that does not exist?
✗ Incorrect
Accessing a missing key with data['age'] raises a KeyError. To avoid this, use data.get('age') or check key existence.
🧠 Conceptual
expert2:30remaining
Which option best explains how Flask handles JSON data in requests?
When a Flask route uses request.get_json(), what does Flask do internally to provide the JSON data?
Attempts:
2 left
💡 Hint
Think about how Flask knows when to parse JSON and what it returns.
✗ Incorrect
Flask checks if Content-Type is application/json, then parses the request body JSON into a Python dictionary returned by get_json(). It does not parse JSON automatically if the header is missing.