Working with Python Dictionaries and Lists
json.loads() returns native Python data structures (lists and dictionaries), not JSON text.
From JSON Text to Python Data
When JSON is received as text, it is not yet a Python list or dictionary that you can navigate with brackets. The json.loads() function parses that text and returns native Python data structures. Once parsing is complete, you work with the result using ordinary Python indexing rather than JSON-specific extraction methods.
Reading the Parsed Shape
The first step in extracting data is to understand the structure returned by parsing. In the source example, the variable info is a list containing two elements. The first element, info[0], is a dictionary with the keys id, x, and name. This tells you which kind of operation to use at each level: use a numeric index for the list, then use a key for the dictionary.
| Structure | Identifier used | Source expression |
|---|---|---|
| List | Numeric position | info[0] |
| Dictionary | Key | item['key'] |
Following Each Bracket
Finding the First User's Name
Retrieve the name stored in the first dictionary inside the parsed variable info.
Start with the list: The variable info is a list. The expression info[0] selects its first element.
Inspect the selected element: The first element is a dictionary containing the keys id, x, and name.
Select the dictionary value: The expression info[0]['name'] uses the name key to retrieve the value associated with it.
The expression info[0]['name'] retrieves 'Chuck'.
info[0] info[0]['name']
Native Python After Parsing
After json.loads() has returned the parsed value, extraction uses Python syntax. You do not need to call the JSON library again to retrieve a value. Standard Python indexing, loops, and methods can work with the returned native structures.
Inspect the structure returned by parsing before writing a longer extraction expression. Knowing whether the outer value is a list or dictionary tells you whether the next operation should use a numeric position or a key.
Mistakes with Nested Access
Treating the parsed result as if it were still raw JSON text.
The parsed result is a native Python structure, so its values should be retrieved with Python indexing.
Fix:
Use expressions such as info[0] and info[0]['name'].Using a dictionary key where a list position is required.
The first level is a list, and the list must be narrowed to one element before its dictionary keys can be used.
Fix:
Use the two stages: first info[0], then the dictionary key.Using a list index where a dictionary key is required.
The selected element is a dictionary whose values are identified by keys.
Fix:
Use info[0]['name'] to retrieve the value under the name key.
Practice the Navigation
Suppose info is the parsed list described in this lesson. Write the Python expression that selects the first element, and then write the chained expression that retrieves the first element's name value.
Hints
- The first list element uses index 0.
- The dictionary value is selected with the name key.
- The complete chained expression combines both bracket operations.
What do you think happens?
What value does info[0]['name'] retrieve in the source example?
Reveal answer
Answer: The first user's name, Chuck
info[0] selects the first dictionary, and ['name'] selects the value associated with that dictionary key.
Key Takeaways
- json.loads() converts JSON text into native Python structures rather than leaving the result as JSON text.
- A list is navigated by numeric position, such as info[0].
- A dictionary is navigated by key, such as item['key'].
- Chained indexing moves through nested structures one level at a time.
- After parsing, ordinary Python indexing, loops, and methods are used for data extraction.
Key Takeaways
- json.loads() returns native Python lists and dictionaries from parsed JSON data.
- Use numeric indexes to select list elements and keys to select dictionary values.
- Read chained expressions from left to right, narrowing the structure at each bracket operation.
- Once parsing is complete, extract values with standard Python syntax rather than JSON-specific methods.