0
0
Flaskframework~20 mins

JSON responses with jsonify in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSONify Master
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 using jsonify?
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}
BAn HTML page showing the dictionary
CA string: "{'name': 'Alice', 'age': 30}"
DA Python dictionary {'name': 'Alice', 'age': 30}
Attempts:
2 left
💡 Hint
jsonify converts Python dicts into JSON strings with correct content-type.
🔧 Debug
intermediate
2:00remaining
Which option causes a TypeError in Flask jsonify usage?
Which of these Flask route return statements will cause a TypeError?
Flask
from flask import jsonify

@app.route('/test')
def test():
    # return statement options below
Areturn jsonify({'valid': True}) + 'extra'
B)}eurT :'dilav'{(yfinosj nruter
Creturn jsonify({'valid': True})
Dreturn jsonify({'valid': True,})
Attempts:
2 left
💡 Hint
jsonify returns a Response object, you cannot add strings to it.
state_output
advanced
2:00remaining
What is the Content-Type header of this Flask jsonify response?
Given this Flask route, what is the Content-Type header sent to the client?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/info')
def info():
    return jsonify({'status': 'ok'})
Aapplication/json
Btext/html; charset=utf-8
Capplication/json; charset=utf-8
Dtext/plain
Attempts:
2 left
💡 Hint
jsonify sets content-type to JSON with UTF-8 charset.
🔧 Debug
advanced
2:00remaining
Why does this Flask jsonify call raise a TypeError?
Examine this code snippet. Why does it raise a TypeError when the route is accessed?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/error')
def error():
    data = {'time': set([1, 2, 3])}
    return jsonify(data)
AMissing return statement causes error
BSets are not JSON serializable, causing TypeError
Cjsonify requires a list, not a dict
DFlask app is not created properly
Attempts:
2 left
💡 Hint
Check if all data types in the dictionary can be converted to JSON.
🧠 Conceptual
expert
2:00remaining
Which option correctly returns a JSON response with a custom status code using jsonify?
You want to return JSON data with a 201 Created status code. Which Flask return statement is correct?
Flask
from flask import jsonify

@app.route('/create')
def create():
    data = {'message': 'Created successfully'}
    # return statement options below
Areturn jsonify(data).with_status(201)
Breturn jsonify(data, status=201)
Creturn jsonify(data).status_code(201)
Dreturn jsonify(data), 201
Attempts:
2 left
💡 Hint
Flask routes can return a tuple with response and status code.