Importing and Using Python Modules
json.loads() transforms a JSON string into native Python structures—lists and dictionaries—not a special JSON type.
From JSON Text to Python Data
A JSON string is text. Before you can work with its contents as a Python list or dictionary, you parse it with json.loads(). The result is native Python data: lists and dictionaries, not a special JSON type. That change determines how you work with the result afterward.
json.loads() transforms a JSON string into native Python structures—lists and dictionaries—not a special JSON type.
The Parsing Step
The parsing process has one important boundary. Before json.loads(), the data is a JSON string. After json.loads(), the result is made of Python lists and dictionaries. The outer shape of the example below is a list, and the item inside that list is a dictionary.
In this example, text holds the JSON string and info holds the result of parsing it. The useful mental model is not that info remains JSON. Instead, info is now a Python list whose first element is a Python dictionary.
Indexing Through Nested Values
Once the JSON text has been parsed, navigate the result with ordinary Python syntax. Use a list index to select an element from a list, then use a dictionary key to select a value from that dictionary. When structures are nested, chain the operations together.
import json text = '[{"name": "Ada"}, {"name": "Grace"}]' info = json.loads(text) name = info[0]['name'] print(name)
The expression info[0]['name'] is a two-step path: first select list element 0, then select the dictionary value under the name key.
Traversing Without JSON Methods
Parsing is the part that uses the json module. Traversing is different. After parsing, you do not need special JSON methods to reach values. Use standard Python operations such as list indexing, dictionary keys, and for loops.
Ada
GraceThe for loop visits the elements of the parsed list. Each person is a dictionary, so person['name'] uses ordinary dictionary-key syntax. The loop does not need a JSON-specific traversal operation.
Common Navigation Mistakes
Treating the parsed result as if it were still JSON text.
json.loads() has already translated the JSON string into native Python lists and dictionaries.
Fix:
Use standard Python syntax: list indexes, dictionary keys, and for loops.Using only the dictionary key and forgetting that the outer structure is a list.
The name key belongs to a dictionary inside the list, not to the outer list itself.
Fix:
Select the list element first, then use the dictionary key, as in info[0]['name'].Stopping the access path before reaching the value.
info[0] selects the dictionary but does not yet select a value from that dictionary.
Fix:
Chain the dictionary-key operation after the list index: info[0]['name'].
Practice the Access Path
Given the parsed value info from the example below, write the Python expression that extracts Grace. info = [{"name": "Ada"}, {"name": "Grace"}]
Hints
- info is a list, so begin with a list index.
- Grace is in the second dictionary, whose list index is 1.
- After selecting that dictionary, use the name key.
Selecting the Second Name
Extract Grace from info = [{"name": "Ada"}, {"name": "Grace"}].
Select the list element: The second dictionary is at list index 1, so begin with info[1].
Select the dictionary value: The desired value is associated with the name key, so add ['name'].
Combine the operations: The complete access path is info[1]['name'].
info[1]['name'] produces Grace.
Working Rule
- Use json.loads() to convert a JSON string into native Python lists and dictionaries.
- After parsing, the result is not a special JSON type.
- Use list indexing to select elements and dictionary keys to select values.
- For nested data, chain access operations such as info[0]['name'].
- Use ordinary Python syntax, including for loops, to traverse parsed data.
Key Takeaways
- json.loads() translates JSON text into native Python lists and dictionaries.
- The parsed result does not require special JSON traversal methods.
- Use list indexes and dictionary keys to follow a path through nested data.
- Chained indexing such as info[0]['name'] extracts a specific nested value.
- Standard Python for loops can traverse the resulting lists.