Challenge - 5 Problems
JSON Parsing Mastery
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 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']}!"})
Attempts:
2 left
💡 Hint
Look at how request.get_json() extracts the JSON body and how the 'name' key is accessed.
✗ Incorrect
The route uses request.get_json() to parse the JSON body. Since the client sends {"name": "Alice"}, data['name'] is 'Alice'. The response JSON contains the greeting message with 'Alice'.
📝 Syntax
intermediate1: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?
Attempts:
2 left
💡 Hint
Remember that get_json is a method you need to call.
✗ Incorrect
request.get_json() is the correct method call to parse JSON data from the request body. request.json is a property but may not always parse correctly. request.json() is invalid syntax. request.get_json without parentheses is a method object, not the result.
🔧 Debug
advanced2: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']
Attempts:
2 left
💡 Hint
Check how request.get_json is used in the code.
✗ Incorrect
The code assigns request.get_json (the method itself) to json_data without calling it. Then it tries to subscript it with ['key'], causing a TypeError because a method object is not subscriptable.
❓ state_output
advanced2: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
Attempts:
2 left
💡 Hint
Look at how data.get('user', {}) works when the key exists.
✗ Incorrect
data.get('user', {}) returns the value associated with 'user' key in the JSON, which is {"id": 5, "name": "Bob"}.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how Flask handles bad requests and JSON parsing errors.
✗ Incorrect
When request.get_json() tries to parse invalid JSON, Flask raises a werkzeug.exceptions.BadRequest error indicating the client sent bad data.