0
0
Flaskframework~10 mins

Accessing JSON data 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 get JSON data from a Flask request.

Flask
data = request.[1]()
Drag options to blanks, or click blank then click option'
Aget_json
Bjson
Cjsonify
Dget_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'jsonify' which is for creating JSON responses, not reading JSON from requests.
Using 'json' which is a module, not a method on request.
Using 'get_data' which returns raw data, not parsed JSON.
2fill in blank
medium

Complete the code to access the value of the 'name' key from JSON data.

Flask
name = data[1]"name"]
Drag options to blanks, or click blank then click option'
A(
B.
C[
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation which does not work on dictionaries.
Using parentheses which are for function calls.
Using curly braces which define dictionaries, not access keys.
3fill in blank
hard

Fix the error in accessing nested JSON data for key 'address' then 'city'.

Flask
city = data["address"][1]"city"]
Drag options to blanks, or click blank then click option'
A[
B.
C(
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation for nested keys.
Using parentheses which cause syntax errors.
Using curly braces which are for dictionary literals.
4fill in blank
hard

Fill both blanks to safely get the 'age' key from JSON data with a default of 0.

Flask
age = data.[1]("age", [2])
Drag options to blanks, or click blank then click option'
Aget
B0
CNone
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which removes the key from the dictionary.
Using 'None' as default which is not the intended default here.
Using dot notation without a method.
5fill in blank
hard

Fill all three blanks to create a Flask route that accepts JSON POST requests and returns the 'status' key.

Flask
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/status', methods=[[1]])
def get_status():
    data = request.[2]()
    status = data.get([3], 'unknown')
    return jsonify({'status': status})
Drag options to blanks, or click blank then click option'
A'POST'
Bget_json
C'status'
D'GET'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'GET' method which does not send JSON in the body.
Using 'jsonify' instead of 'get_json' to read data.
Using wrong key name or missing quotes.