Bird
0
0

You want to test a Flask route that returns JSON data. Which code snippet correctly tests the JSON response?

hard📝 Application Q8 of 15
Flask - Testing Flask Applications
You want to test a Flask route that returns JSON data. Which code snippet correctly tests the JSON response?
Aresponse = client.get('/api'); data = response.json()
Bresponse = client.get('/api'); data = response.get_json()
Cresponse = client.get('/api'); data = json.loads(response.data)
Dresponse = client.get('/api'); data = response.data.json()
Step-by-Step Solution
Solution:
  1. Step 1: Recall Flask test client JSON helper

    Flask's response object has a method get_json() to parse JSON responses easily.
  2. Step 2: Compare options

    response = client.get('/api'); data = response.get_json() uses get_json(), which is the correct and simple way. response = client.get('/api'); data = json.loads(response.data) is close but requires importing json and decoding bytes first.
  3. Final Answer:

    response = client.get('/api'); data = response.get_json() -> Option B
  4. Quick Check:

    Use response.get_json() to parse JSON [OK]
Quick Trick: Use response.get_json() to get JSON data [OK]
Common Mistakes:
MISTAKES
  • Using response.json() which does not exist
  • Trying to call json() on bytes data
  • Not decoding bytes before json.loads

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes