Challenge - 5 Problems
JSON Response Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Flask route return as JSON?
Consider this Flask route code. What JSON response will the client receive when accessing
/data?Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/data') def data(): return jsonify({'name': 'Alice', 'age': 30})
Attempts:
2 left
💡 Hint
Remember that jsonify converts Python dicts to JSON strings with double quotes.
✗ Incorrect
The jsonify function returns a JSON response with keys and string values in double quotes, matching option C exactly.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in Flask JSON response?
Which Flask route code snippet will cause a syntax error when returning JSON?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/test') def test(): # Choose the problematic return statement
Attempts:
2 left
💡 Hint
Look for missing or mismatched quotes in the dictionary keys or values.
✗ Incorrect
Option B has a missing closing quote around the key 'missing_quote', causing a syntax error.
❓ state_output
advanced2:00remaining
What is the JSON output of this Flask route with nested data?
Given this Flask route, what JSON does it return?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/nested') def nested(): data = {'user': {'id': 1, 'name': 'Bob'}, 'active': True} return jsonify(data)
Attempts:
2 left
💡 Hint
Boolean values in JSON are lowercase true/false, not strings.
✗ Incorrect
Option D correctly shows nested objects and boolean true without quotes, matching Python dict to JSON conversion.
🔧 Debug
advanced2:00remaining
Why does this Flask JSON response raise a TypeError?
This Flask route raises a TypeError when returning JSON. Why?
Flask
from flask import Flask, jsonify app = Flask(__name__) class Custom: pass @app.route('/error') def error(): obj = Custom() return jsonify({'obj': obj})
Attempts:
2 left
💡 Hint
Think about what types jsonify can convert to JSON.
✗ Incorrect
jsonify cannot convert custom Python objects unless they are converted to JSON serializable types like dict or string.
🧠 Conceptual
expert2:00remaining
Which option correctly sets a custom HTTP status code with JSON response in Flask?
You want to return JSON data with a 201 Created status code. Which Flask return statement is correct?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/create') def create(): data = {'message': 'Created successfully'}
Attempts:
2 left
💡 Hint
Flask allows returning a tuple with response and status code.
✗ Incorrect
Returning (jsonify(data), 201) sets the response body and HTTP status code correctly.