Bird
Raised Fist0
Flaskframework~20 mins

JSON request parsing in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON Parsing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Flask route when receiving a JSON POST?
Consider this Flask route that expects JSON data with a key 'name'. What will be the response if the client sends {"name": "Alice"} as JSON?
Flask
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/greet', methods=['POST'])
def greet():
    data = request.get_json()
    return jsonify({'message': f"Hello, {data['name']}!"})
AKeyError
B{"message": "Hello, None!"}
C{"message": "Hello, Alice!"}
DTypeError
Attempts:
2 left
💡 Hint
Look at how request.get_json() extracts the JSON body and how the 'name' key is accessed.
📝 Syntax
intermediate
1:30remaining
Which option correctly parses JSON from a Flask request?
You want to parse JSON data sent in a POST request in Flask. Which code snippet correctly gets the JSON data?
Adata = request.json()
Bdata = request.get_json()
Cdata = request.get_json
Ddata = request.json
Attempts:
2 left
💡 Hint
Remember that get_json is a method you need to call.
🔧 Debug
advanced
2:30remaining
Why does this Flask route raise a TypeError when parsing JSON?
Given this Flask route, why does it raise a TypeError when a client sends JSON data?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/data', methods=['POST'])
def data():
    json_data = request.get_json
    return json_data['key']
Arequest.get_json is a method, missing parentheses causes TypeError when subscripted
Brequest.get_json returns None because no JSON sent
CKeyError because 'key' does not exist in JSON
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint
Check how request.get_json is used in the code.
❓ state_output
advanced
2:00remaining
What is the value of 'user' after this Flask route processes JSON?
In this Flask route, what will be the value of the variable 'user' after parsing the JSON body {"user": {"id": 5, "name": "Bob"}}?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/user', methods=['POST'])
def user_route():
    data = request.get_json()
    user = data.get('user', {})
    return user
A{"id": 5, "name": "Bob"}
B"user"
C{}
DNone
Attempts:
2 left
💡 Hint
Look at how data.get('user', {}) works when the key exists.
🧠 Conceptual
expert
2:00remaining
What error occurs if Flask receives invalid JSON with request.get_json()?
If a Flask route calls request.get_json() but the client sends malformed JSON, what error will Flask raise?
AValueError
BKeyError
CTypeError
Dwerkzeug.exceptions.BadRequest
Attempts:
2 left
💡 Hint
Think about how Flask handles bad requests and JSON parsing errors.

Practice

(1/5)
1. What is the correct Flask method to parse JSON data sent in a client request?
easy
A. request.data.json()
B. request.json()
C. request.parse_json()
D. request.get_json()

Solution

  1. Step 1: Understand Flask request object

    The Flask request object provides methods to access incoming request data.
  2. Step 2: Identify JSON parsing method

    The method get_json() is designed to parse JSON data from the request body safely.
  3. Final Answer:

    request.get_json() -> Option D
  4. Quick Check:

    JSON parsing method = request.get_json() [OK]
Hint: Use request.get_json() to read JSON data safely [OK]
Common Mistakes:
  • Using request.json() which is not a method
  • Trying to parse JSON from request.data directly
  • Using non-existent methods like parse_json()
2. Which of the following is the correct way to import and use JSON parsing in a Flask route?
easy
A. from flask import request @app.route('/data') def data(): data = request.get_json() return data
B. import flask @app.route('/data') def data(): data = flask.request.json() return data
C. from flask import request @app.route('/data') def data(): data = request.json return data
D. from flask import request @app.route('/data') def data(): data = request.get_json return data

Solution

  1. Step 1: Check import and usage

    Correct usage imports request from flask and calls request.get_json() as a method.
  2. Step 2: Identify correct method call

    from flask import request @app.route('/data') def data(): data = request.get_json() return data correctly calls request.get_json() with parentheses to parse JSON.
  3. Final Answer:

    Option A code snippet -> Option A
  4. Quick Check:

    Correct import and method call = from flask import request @app.route('/data') def data(): data = request.get_json() return data [OK]
Hint: Always call get_json() with parentheses after importing request [OK]
Common Mistakes:
  • Calling get_json without parentheses
  • Using request.json() which does not exist
  • Accessing request.json property which is not standard
3. Given this Flask route code, what will be the output if the client sends JSON {"name": "Alice"}?
from flask import Flask, request
app = Flask(__name__)

@app.route('/user', methods=['POST'])
def user():
    data = request.get_json()
    return f"Hello, {data['name']}!"
medium
A. "Hello, Alice!"
B. KeyError: 'name'
C. TypeError: 'NoneType' object is not subscriptable
D. SyntaxError

Solution

  1. Step 1: Parse JSON from request

    The client sends valid JSON with key "name" and value "Alice". request.get_json() parses it into a Python dict.
  2. Step 2: Access dictionary key and return string

    The code accesses data['name'] which exists, so it returns the formatted string "Hello, Alice!".
  3. Final Answer:

    "Hello, Alice!" -> Option A
  4. Quick Check:

    Valid JSON with 'name' key = "Hello, Alice!" [OK]
Hint: If JSON has key, get_json() returns dict to access keys [OK]
Common Mistakes:
  • Assuming get_json() returns None if JSON is missing
  • Not handling missing keys causing KeyError
  • Confusing get_json() with request.data string
4. What is the main issue with this Flask route when parsing JSON?
@app.route('/submit', methods=['POST'])
def submit():
    data = request.get_json()
    return data['email']
medium
A. request.get_json() is not a valid method
B. It may raise a KeyError if 'email' is missing in JSON
C. The route does not accept POST requests
D. It returns JSON instead of a string

Solution

  1. Step 1: Analyze JSON key access

    The code accesses data['email'] without checking if 'email' exists in the JSON data.
  2. Step 2: Identify potential error

    If the client sends JSON without 'email', this causes a KeyError and crashes the route.
  3. Final Answer:

    It may raise a KeyError if 'email' is missing in JSON -> Option B
  4. Quick Check:

    Missing key access causes KeyError = It may raise a KeyError if 'email' is missing in JSON [OK]
Hint: Always check keys exist before accessing JSON data [OK]
Common Mistakes:
  • Assuming get_json() always returns dict with all keys
  • Not handling missing or invalid JSON gracefully
  • Confusing method errors with key errors
5. How can you modify this Flask route to safely parse JSON and handle missing or invalid JSON without crashing?
@app.route('/info', methods=['POST'])
def info():
    data = request.get_json()
    return data['id']
hard
A. Add if 'id' in data: without checking if data is None
B. Replace get_json() with request.json() to avoid errors
C. Use data = request.get_json(silent=True) and check if data is None before accessing keys
D. Use request.get_json(force=True) to force JSON parsing

Solution

  1. Step 1: Use silent=True to avoid exceptions

    Calling request.get_json(silent=True) returns None instead of raising an error if JSON is missing or invalid.
  2. Step 2: Check if data is None before accessing keys

    Before accessing data['id'], check if data is not None to avoid crashes.
  3. Final Answer:

    Use data = request.get_json(silent=True) and check for None -> Option C
  4. Quick Check:

    Silent parsing + None check = safe JSON access [OK]
Hint: Use silent=True and check None to avoid JSON parsing errors [OK]
Common Mistakes:
  • Using request.json() which is not a method
  • Not checking if data is None before key access
  • Using force=True which may cause errors if content is not JSON
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard