300-435 · Question #71
A web service returns a JSON object to your Python script. What is the most logical object in which to store the JSON response?
The correct answer is B. a dictionary. The most logical Python object to store a JSON response from a web service is a dictionary, as JSON objects directly map to Python dictionaries.
Question
Options
- Aan integer
- Ba dictionary
- Ca tuple
- Da string
- Ea list
How the community answered
(39 responses)- B95% (37)
- C3% (1)
- E3% (1)
Why each option
The most logical Python object to store a JSON response from a web service is a dictionary, as JSON objects directly map to Python dictionaries.
An integer can only hold a single numerical value, which is insufficient for a structured JSON object.
When a web service returns a JSON object, the Python `json` module, or methods like `response.json()` from the `requests` library, will parse it into a Python dictionary. This is because JSON objects are composed of key-value pairs, which directly correspond to the structure of a Python dictionary.
A tuple is an immutable ordered sequence and does not naturally represent the key-value structure of a JSON object.
A string can hold the raw JSON text, but it must be parsed to become an accessible Python data structure, making it less logical for direct storage and manipulation.
A list is an ordered sequence of items, whereas a JSON object is an unordered collection of key-value pairs, which is better represented by a dictionary.
Concept tested: JSON to Python data type mapping
Source: https://docs.python.org/3/library/json.html
Topics
Community Discussion
No community discussion yet for this question.