0
0
Flaskframework~10 mins

JSON responses with jsonify in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function that helps create JSON responses in Flask.

Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Arequest
Brender_template
CFlask
Djsonify
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Flask instead of jsonify
Using render_template which is for HTML templates
Importing request which is for incoming data
2fill in blank
medium

Complete the code to return a JSON response with a message using jsonify.

Flask
return [1]({'message': 'Hello, world!'})
Drag options to blanks, or click blank then click option'
Ajsonify
Brender_template
Cmake_response
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using render_template which returns HTML
Using json which is a module, not a response function
Using make_response without jsonify
3fill in blank
hard

Fix the error in the code to correctly return a JSON response with a status code 201.

Flask
return jsonify({'status': 'created'}), [1]
Drag options to blanks, or click blank then click option'
A201
B200
C400
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which is generic success
Using 400 which means bad request
Using 500 which means server error
4fill in blank
hard

Fill both blanks to create a Flask route that returns a JSON response with a list of users.

Flask
@app.route('/users')
def get_users():
    users = ['Alice', 'Bob', 'Charlie']
    return [1]({'users': [2])
Drag options to blanks, or click blank then click option'
Ajsonify
Brender_template
Cusers
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using render_template which returns HTML
Passing 'request' instead of the users list
Not using jsonify for JSON response
5fill in blank
hard

Fill all three blanks to create a Flask route that accepts POST requests and returns a JSON response with the posted data.

Flask
@app.route('/submit', methods=[[1]])
def submit():
    data = [2].get_json()
    return [3]({'received': data})
Drag options to blanks, or click blank then click option'
A'POST'
Brequest
Cjsonify
D'GET'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'GET' instead of 'POST' in methods
Using 'jsonify' as the request object
Not using jsonify to return JSON response