0
0
Flaskframework~20 mins

Accessing JSON data in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Data Mastery in Flask
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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']}!"})
A{"greeting": "Hello, 30!"}
B{"greeting": "Hello, None!"}
CKeyError exception
D{"greeting": "Hello, Alice!"}
Attempts:
2 left
💡 Hint
Look at how the JSON data is accessed by key 'name' in the dictionary.
📝 Syntax
intermediate
1: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 = ???
Adata['user']['info']['email']
Bdata['user']['email']
Cdata['info']['email']
Ddata['email']
Attempts:
2 left
💡 Hint
Follow the nested keys step by step from the JSON structure.
🔧 Debug
advanced
2: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']
AKeyError because 'key' does not exist in data
Brequest.json is None if Content-Type is not application/json
CSyntaxError due to missing parentheses
DTypeError because data is a string
Attempts:
2 left
💡 Hint
Check the request headers and how Flask parses JSON automatically.
state_output
advanced
2: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})
A{"age": 0}
B{"age": null}
CKeyError exception
D{"age": ""}
Attempts:
2 left
💡 Hint
What happens when you access a dictionary key that does not exist?
🧠 Conceptual
expert
2: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?
AFlask reads the request body, checks Content-Type header, and parses JSON into a Python dict
BFlask requires manual parsing of JSON using json.loads(request.data)
CFlask uses the URL parameters to build JSON data
DFlask automatically converts all request data into JSON regardless of Content-Type
Attempts:
2 left
💡 Hint
Think about how Flask knows when to parse JSON and what it returns.