The jsonify function formats Python data structures into a JSON response with the correct content type application/json. It helps send data to the client in a standard JSON format.
Click to reveal answer
beginner
How do you return a JSON response with a custom status code in Flask?
Use jsonify(data), status_code. For example, return jsonify({'message': 'ok'}), 201 sends JSON with HTTP status 201.
Click to reveal answer
beginner
What content type does Flask set automatically when using jsonify?
Flask sets the content type to application/json automatically when you use jsonify. This tells the browser or client that the response is JSON data.
Click to reveal answer
intermediate
Why should you avoid returning a plain Python dictionary directly from a Flask route?
Returning a plain dictionary does not set the content type to JSON automatically. This can cause clients to misinterpret the response. Using jsonify ensures proper JSON formatting and headers.
Click to reveal answer
intermediate
How can you include custom headers in a JSON response in Flask?
Create the response with jsonify, then use response.headers['Header-Name'] = 'value'. Finally, return the response object.
Click to reveal answer
Which Flask function is best for sending JSON responses?
Ajsonify
Brender_template
Credirect
Dsend_file
✗ Incorrect
jsonify formats data as JSON and sets the correct content type.
What content type does jsonify set in the response?
Aapplication/json
Btext/html
Ctext/plain
Dapplication/xml
✗ Incorrect
jsonify sets application/json to indicate JSON data.
How do you return a JSON response with status code 404 in Flask?
Areturn render_template('404.html'), 404
Breturn {'error': 'Not found'}, 404
Creturn jsonify({'error': 'Not found'}), 404
Dreturn redirect('/404')
✗ Incorrect
Use jsonify with the status code to send JSON and HTTP status.
What happens if you return a plain dictionary from a Flask route?
AFlask automatically converts it to JSON
BFlask returns it as plain text without JSON headers
CFlask raises an error
DFlask redirects to another page
✗ Incorrect
Returning a dict directly does not set JSON content type, so clients may not parse it as JSON.
How can you add a custom header to a JSON response in Flask?
AAdd header to the dictionary before jsonify
BCustom headers are not allowed in JSON responses
CPass headers as a second argument to jsonify
DUse <code>response.headers['Name'] = 'value'</code> on the response object
✗ Incorrect
You modify the response object headers after calling jsonify.
Explain how to send a JSON response with a custom HTTP status code in Flask.
Think about how Flask routes return data and status.
You got /3 concepts.
Describe why using jsonify is better than returning a plain dictionary in Flask routes.
Consider how browsers and clients understand response data.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using jsonify() in a Flask app?
easy
A. To connect to a database
B. To create HTML pages dynamically
C. To handle user authentication
D. To format Python data as a JSON response easily
Solution
Step 1: Understand the role of jsonify()
The jsonify() function converts Python data structures into JSON format for HTTP responses.
Step 2: Identify the correct purpose
It is not used for HTML, database, or authentication tasks but specifically for JSON response formatting.
Final Answer:
To format Python data as a JSON response easily -> Option D
Quick Check:
jsonify() formats data as JSON [OK]
Hint: jsonify() turns Python data into JSON response fast [OK]
Common Mistakes:
Thinking jsonify() creates HTML pages
Confusing jsonify() with database functions
Assuming jsonify() handles user login
2. Which of the following is the correct way to return a JSON response in Flask?
easy
A. return jsonify({'key': 'value'})
B. return json.dumps({'key': 'value'})
C. return render_template('data.json')
D. return Response({'key': 'value'})
Solution
Step 1: Review Flask JSON response syntax
Flask provides jsonify() to return JSON responses with correct headers automatically.
Step 2: Compare options
return json.dumps({'key': 'value'}) returns a string without proper headers, C returns a template, D uses Response incorrectly.
Final Answer:
return jsonify({'key': 'value'}) -> Option A
Quick Check:
Use jsonify() to return JSON response [OK]
Hint: Use jsonify() to return JSON with correct headers [OK]
Common Mistakes:
Using json.dumps() without setting content-type
Returning templates instead of JSON
Using Response without JSON serialization
3. What will be the output of this Flask route?
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/data')
def data():
info = {'name': 'Alice', 'age': 30}
return jsonify(info)
medium
A. A Python dictionary printed as string
B. An HTML page showing the dictionary
C. A JSON response: {"name": "Alice", "age": 30}
D. A server error due to missing return type
Solution
Step 1: Analyze the route function
The function returns jsonify(info), which sends a JSON response with the dictionary data.
Step 2: Understand jsonify output
jsonify converts the dict to JSON string and sets content-type to application/json.
Final Answer:
A JSON response: {"name": "Alice", "age": 30} -> Option C
Quick Check:
jsonify returns JSON response [OK]
Hint: jsonify() returns JSON string with headers, not plain dict [OK]
Common Mistakes:
Expecting Python dict printed as string
Thinking it returns HTML
Assuming server error without return type
4. Identify the error in this Flask code snippet:
from flask import Flask, jsonify
import json
app = Flask(__name__)
@app.route('/info')
def info():
data = {'status': 'ok'}
return json.dumps(data)
medium
A. Route decorator syntax is wrong
B. json.dumps() returns a string without proper response headers
C. Flask app is not created correctly
D. jsonify() is missing import
Solution
Step 1: Check the return statement
The code returns json.dumps(data), which is a JSON string but lacks HTTP headers for JSON content.
Step 2: Understand Flask JSON response best practice
Using jsonify() sets content-type headers correctly; returning json.dumps() alone can cause client issues.
Final Answer:
json.dumps() returns a string without proper response headers -> Option B
Quick Check:
json.dumps() lacks JSON headers [OK]
Hint: Use jsonify(), not json.dumps(), to send JSON response [OK]
Common Mistakes:
Returning json.dumps() string without headers
Forgetting to import jsonify
Misusing route decorator
5. You want to return a JSON response with a custom status code 201 and a message. Which is the correct way to do this in Flask?
hard
A. return jsonify({'message': 'Created'}), 201
B. return Response(json.dumps({'message': 'Created'}), status=201)
C. return jsonify({'message': 'Created', 'status': 201})
D. return {'message': 'Created'}, 201
Solution
Step 1: Understand how to set status code with jsonify
Flask allows returning a tuple with (response, status_code) where response can be jsonify output.
Step 2: Evaluate options
return jsonify({'message': 'Created'}), 201 correctly returns jsonify with status 201. return Response(json.dumps({'message': 'Created'}), status=201) misses content-type headers. return jsonify({'message': 'Created', 'status': 201}) puts status inside JSON data, not HTTP status. return {'message': 'Created'}, 201 returns dict without jsonify.
Final Answer:
return jsonify({'message': 'Created'}), 201 -> Option A
Quick Check:
Use jsonify() with status code tuple [OK]
Hint: Return (jsonify(data), status_code) to set HTTP status [OK]
Common Mistakes:
Putting status code inside JSON data
Using Response without headers
Returning dict without jsonify
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.