0
0
Flaskframework~5 mins

Accessing JSON data in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What Flask method do you use to get JSON data sent in a POST request?
You use request.get_json() to access JSON data sent in a POST request in Flask.
Click to reveal answer
beginner
How do you import the object needed to access JSON data in Flask?
You import <code>request</code> from <code>flask</code> like this: <code>from flask import request</code>.
Click to reveal answer
beginner
What type of data does request.get_json() return?
It returns a Python dictionary representing the JSON data sent by the client.
Click to reveal answer
intermediate
What happens if the incoming request does not contain valid JSON and you call request.get_json()?
By default, it returns None if the JSON is missing or invalid.
Click to reveal answer
intermediate
How can you safely access a key from JSON data in Flask to avoid errors if the key is missing?
Use the dictionary .get() method on the JSON data, like data.get('key'), which returns None if the key is missing.
Click to reveal answer
Which Flask object do you use to access JSON data sent by a client?
Arequest
Bresponse
Cjsonify
Dsession
What does request.get_json() return if the request has no JSON data?
AAn error is raised
BAn empty dictionary {}
CNone
DA string with raw data
How do you import the request object in Flask?
Aimport request
Bimport flask.request
Cfrom flask.request import *
Dfrom flask import request
If you want to safely get a value from JSON data without risking a KeyError, what should you do?
AUse data['key']
BUse data.get('key')
CUse data.key
DUse data.fetch('key')
Which HTTP method usually carries JSON data that you want to access with request.get_json()?
APOST
BHEAD
CDELETE
DGET
Explain how to access JSON data sent by a client in a Flask route.
Think about the Flask object that holds request data and the method to parse JSON.
You got /3 concepts.
    Describe what happens if you call request.get_json() but the client sends no JSON data.
    Consider the default behavior of get_json when JSON is missing.
    You got /3 concepts.