Bird
Raised Fist0
Flaskframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the jsonify function in Flask do?
easy
A. Converts Python data to JSON and sets response headers correctly
B. Converts JSON data to Python objects
C. Creates HTML responses from Python dictionaries
D. Sends plain text responses without headers

Solution

  1. Step 1: Understand the purpose of jsonify

    The jsonify function is designed to convert Python data structures like dictionaries or lists into JSON format.
  2. Step 2: Recognize response headers setting

    It also automatically sets the HTTP response headers to indicate the content type is JSON, which is important for browsers or clients to understand the data format.
  3. Final Answer:

    Converts Python data to JSON and sets response headers correctly -> Option A
  4. Quick Check:

    jsonify converts data to JSON with headers [OK]
Hint: jsonify = Python data to JSON response with headers [OK]
Common Mistakes:
  • Thinking jsonify converts JSON to Python
  • Assuming jsonify creates HTML
  • Ignoring response headers setup
2. Which of the following is the correct way to return a JSON response with jsonify in Flask?
easy
A. return jsonify('name', 'Alice', 'age', 30)
B. return json.dumps({'name': 'Alice', 'age': 30})
C. return jsonify({'name': 'Alice', 'age': 30})
D. return jsonify('name': 'Alice', 'age': 30)

Solution

  1. Step 1: Check the syntax for jsonify with a dictionary

    The correct syntax is to pass a dictionary inside parentheses: jsonify({'key': value}).
  2. Step 2: Identify the correct option

    return jsonify({'name': 'Alice', 'age': 30}) correctly uses a dictionary inside jsonify. return json.dumps({'name': 'Alice', 'age': 30}) uses json.dumps which returns a string, not a Flask response. return jsonify('name': 'Alice', 'age': 30) has incorrect syntax missing braces. return jsonify('name', 'Alice', 'age', 30) passes multiple positional arguments, causing a TypeError.
  3. Final Answer:

    return jsonify({'name': 'Alice', 'age': 30}) -> Option C
  4. Quick Check:

    jsonify expects a dict inside parentheses [OK]
Hint: Use jsonify with a dictionary inside parentheses [OK]
Common Mistakes:
  • Using json.dumps instead of jsonify
  • Passing key-value pairs without braces
  • Passing multiple positional arguments to jsonify
3. What will be the output content type of this Flask route?
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/data')
def data():
    return jsonify({'success': True, 'count': 5})
medium
A. application/json
B. text/plain
C. text/html
D. application/xml

Solution

  1. Step 1: Identify the response type from jsonify

    The jsonify function returns a Flask response with the content type set to application/json.
  2. Step 2: Confirm the content type for JSON data

    Since the route returns jsonify({'success': True, 'count': 5}), the content type will be application/json.
  3. Final Answer:

    application/json -> Option A
  4. Quick Check:

    jsonify sets content type to application/json [OK]
Hint: jsonify always sets content type to application/json [OK]
Common Mistakes:
  • Assuming default is text/html
  • Confusing with plain text content type
  • Thinking jsonify returns XML
4. Identify the error in this Flask route using jsonify:
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/info')
def info():
    data = {'name': 'Bob', 'age': 25}
    return jsonify(data['name'], data['age'])
medium
A. Route decorator syntax is wrong
B. Missing parentheses in jsonify call
C. Flask app not initialized correctly
D. jsonify expects a single dictionary or list, not multiple arguments

Solution

  1. Step 1: Analyze the jsonify call arguments

    The code calls jsonify(data['name'], data['age']) passing two separate arguments instead of one dictionary or list.
  2. Step 2: Understand jsonify argument requirements

    jsonify expects a single argument like a dictionary, list, or keyword arguments, not multiple positional arguments.
  3. Final Answer:

    jsonify expects a single dictionary or list, not multiple arguments -> Option D
  4. Quick Check:

    jsonify needs one dict/list argument [OK]
Hint: Pass one dict or list to jsonify, not multiple values [OK]
Common Mistakes:
  • Passing multiple arguments to jsonify
  • Confusing jsonify with print function
  • Ignoring argument structure requirements
5. You want to return a JSON response with a list of users and a status message using jsonify. Which code correctly combines a list and a message in the JSON response?
hard
A. return jsonify(['Ann', 'Ben'], status='ok')
B. return jsonify({'users': ['Ann', 'Ben'], 'status': 'ok'})
C. return jsonify(users=['Ann', 'Ben'], 'status': 'ok')
D. return jsonify({'users': ['Ann', 'Ben']}, {'status': 'ok'})

Solution

  1. Step 1: Understand how to combine data in jsonify

    To send multiple pieces of data, combine them into a single dictionary with keys and values.
  2. Step 2: Evaluate each option

    return jsonify({'users': ['Ann', 'Ben'], 'status': 'ok'}) correctly creates one dictionary with keys 'users' and 'status'. return jsonify(['Ann', 'Ben'], status='ok') passes a list and a keyword argument, which is invalid. return jsonify(users=['Ann', 'Ben'], 'status': 'ok') mixes keyword arguments and dictionary syntax incorrectly. return jsonify({'users': ['Ann', 'Ben']}, {'status': 'ok'}) passes two dictionaries separately, which is not allowed.
  3. Final Answer:

    return jsonify({'users': ['Ann', 'Ben'], 'status': 'ok'}) -> Option B
  4. Quick Check:

    Combine all data in one dict for jsonify [OK]
Hint: Put all data in one dictionary before jsonify [OK]
Common Mistakes:
  • Passing multiple arguments instead of one dict
  • Mixing keyword args and dict syntax wrongly
  • Trying to jsonify multiple separate dicts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard