Converting Python Objects to JSON with json.dumps()
json.loads() transforms a JSON string into native Python structures—lists and dictionaries—not a special JSON type.
From Text to Structures
A JSON string is text. Before you can work with the information inside it as Python data, json.loads() transforms that text into native Python structures. The result is not a special JSON object: it is made from ordinary Python lists and dictionaries.
The important change is the representation: JSON text becomes Python data that can be handled with lists, dictionaries, indexes, keys, and for loops.
A Nested Python Shape
After parsing, the structure of the data matters. A list can contain dictionaries, and a dictionary can contain a list. You navigate by following that structure one step at a time. If a list contains several dictionary records, an index selects one record; a dictionary key then selects a value inside that record.
Following Indexes and Keys
import json text = '[{"name": "Ari"}, {"name": "Bea"}]' records = json.loads(text) first_name = records[0]["name"] print(first_name)
The expression records[0]["name"] is chained access. First, [0] operates on the list. The result is a dictionary, so ["name"] then operates on that dictionary. Each part of the expression matches the next container in the parsed structure.
Parsing Versus Serialization
| Operation | Starting data | Result |
|---|---|---|
| json.loads() | JSON string | Native Python lists and dictionaries |
| json.dumps() | Python object | JSON text |
The names are easy to confuse. json.loads() is the operation used when JSON text must become Python structures that you can navigate. json.dumps() is the opposite direction: it converts a Python object to JSON text. This article's navigation steps begin after json.loads(), because the result is ordinary Python data.
Mistakes with Parsed Data
Treating the parsed result as a special JSON type
json.loads() translates the JSON string into native Python lists and dictionaries.
Fix:
Use standard Python list indexing, dictionary keys, and for loops.Using only a dictionary key when the outer structure is a list
The first access must match the outer container. A list is navigated with an index.
Fix:
Select an element first, then use its dictionary key, as in records[0]["name"].Stopping the access chain too early
This selects the first dictionary but does not yet select the value stored under a key.
Fix:
Add the dictionary key when a specific field is needed: records[0]["name"].
Before writing an access expression, identify the current container. Ask whether it is a list or a dictionary. Use an index for a list and a key for a dictionary, then repeat the process for the next nested value.
Practice the Path
Suppose parsed_data is a Python list whose first element is a dictionary containing the key "city". Write the standard Python expression that selects the value for "city" in the first element.
Hints
- The outer structure is a list, so begin with an index.
- The first element uses index 0.
- The selected dictionary uses the key "city".
Selecting a Nested City
Find the value associated with the city key in the first dictionary of parsed_data.
Identify the outer structure: parsed_data is a list, so use a list index first.
Select the first element: The first element is selected with index 0, producing the first dictionary.
Select the dictionary value: Use the city key on that dictionary.
parsed_data[0]["city"]
What to Remember
- json.loads() transforms a JSON string into native Python lists and dictionaries.
- After parsing, the data is navigated with standard Python syntax.
- Use list indexes to select list elements and dictionary keys to select dictionary values.
- Chained access follows nested structures one container at a time, such as info[0]["name"].
- json.loads() reads JSON text into Python structures, while json.dumps() converts Python objects into JSON text.
Key Takeaways
- json.loads() changes JSON text into ordinary Python lists and dictionaries.
- Parsed JSON does not require special traversal methods.
- Use indexes for lists, keys for dictionaries, and chained access for nested data.
- The expression info[0]["name"] selects a key from the first dictionary in a list.
- json.dumps() moves in the opposite direction by converting Python objects to JSON text.