0
0
Flaskframework~20 mins

JSON request parsing in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Parsing Mastery
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 receiving a JSON POST?
Consider this Flask route that expects JSON data with a key 'name'. What will be the response if the client sends {"name": "Alice"} as JSON?
Flask
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/greet', methods=['POST'])
def greet():
    data = request.get_json()
    return jsonify({'message': f"Hello, {data['name']}!"})
AKeyError
B{"message": "Hello, None!"}
C{"message": "Hello, Alice!"}
DTypeError
Attempts:
2 left
💡 Hint
Look at how request.get_json() extracts the JSON body and how the 'name' key is accessed.
📝 Syntax
intermediate
1:30remaining
Which option correctly parses JSON from a Flask request?
You want to parse JSON data sent in a POST request in Flask. Which code snippet correctly gets the JSON data?
Adata = request.json()
Bdata = request.get_json()
Cdata = request.get_json
Ddata = request.json
Attempts:
2 left
💡 Hint
Remember that get_json is a method you need to call.
🔧 Debug
advanced
2:30remaining
Why does this Flask route raise a TypeError when parsing JSON?
Given this Flask route, why does it raise a TypeError when a client sends JSON data?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/data', methods=['POST'])
def data():
    json_data = request.get_json
    return json_data['key']
Arequest.get_json is a method, missing parentheses causes TypeError when subscripted
Brequest.get_json returns None because no JSON sent
CKeyError because 'key' does not exist in JSON
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint
Check how request.get_json is used in the code.
state_output
advanced
2:00remaining
What is the value of 'user' after this Flask route processes JSON?
In this Flask route, what will be the value of the variable 'user' after parsing the JSON body {"user": {"id": 5, "name": "Bob"}}?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/user', methods=['POST'])
def user_route():
    data = request.get_json()
    user = data.get('user', {})
    return user
A{"id": 5, "name": "Bob"}
B"user"
C{}
DNone
Attempts:
2 left
💡 Hint
Look at how data.get('user', {}) works when the key exists.
🧠 Conceptual
expert
2:00remaining
What error occurs if Flask receives invalid JSON with request.get_json()?
If a Flask route calls request.get_json() but the client sends malformed JSON, what error will Flask raise?
AValueError
BKeyError
CTypeError
Dwerkzeug.exceptions.BadRequest
Attempts:
2 left
💡 Hint
Think about how Flask handles bad requests and JSON parsing errors.