0
0
Flaskframework~20 mins

JSON response formatting in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Response Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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})
A'{"name": "Alice", "age": 30}'
B["name", "Alice", "age", 30]
C{"name": "Alice", "age": 30}
D{name: Alice, age: 30}
Attempts:
2 left
💡 Hint
Remember that jsonify converts Python dicts to JSON strings with double quotes.
📝 Syntax
intermediate
2: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
Areturn jsonify({'valid': True})
Breturn jsonify({'missing_quote: True})
Creturn jsonify({'number': 123})
Dreturn jsonify({'list': [1, 2, 3]})
Attempts:
2 left
💡 Hint
Look for missing or mismatched quotes in the dictionary keys or values.
state_output
advanced
2: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)
A{"user": {"id": 1, "name": "Bob"}, "active": "true"}
B{"user": {"id": "1", "name": "Bob"}, "active": "True"}
C{"user": [1, "Bob"], "active": true}
D{"user": {"id": 1, "name": "Bob"}, "active": true}
Attempts:
2 left
💡 Hint
Boolean values in JSON are lowercase true/false, not strings.
🔧 Debug
advanced
2: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})
ACustom object is not JSON serializable
BMissing return statement
CFlask app not initialized
Djsonify requires a list, not dict
Attempts:
2 left
💡 Hint
Think about what types jsonify can convert to JSON.
🧠 Conceptual
expert
2: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'}
Areturn jsonify(data), 201
Breturn jsonify(data, status=201)
Creturn jsonify(data).status_code(201)
Dreturn jsonify(data).with_status(201)
Attempts:
2 left
💡 Hint
Flask allows returning a tuple with response and status code.