Concepts / Iterating Over Parsed Data Structures

Iterating Over Parsed Data Structures

json.loads() returns native Python data structures (lists and dictionaries), not JSON text.

  • Programming

From Text to Structure

When JSON data arrives as text, it is a string of JSON characters. Calling json.loads() parses that text and returns native Python data structures. In the example used here, the result is a list containing dictionaries. Once parsing is complete, you navigate the result with ordinary Python indexing rather than JSON-specific extraction methods.

python
inputreturnsJSON textcharactersjson.loads()parseinfolist of dictionaries
What changes when json.loads() converts JSON text into a navigable Python structure?

The important state change is the value of info. Before parsing, the data is JSON text. After parsing, info is a Python list. Its elements are dictionaries, and each dictionary contains fields such as id, x, and name. That structure is what makes list indexes and dictionary keys available.

Following the Index Path

Extraction is a two-step indexing process. First, use a list index to select one element from info. Then, use a dictionary key to select a value from that dictionary. The expression info[0] selects the first list element. The expression info[0]['name'] continues one level deeper and selects the name value in the first dictionary.

[0]['name']infolistinfo[0]dictionaryinfo[0]['name']Chuck
What sequence of indexes and dictionary keys reaches the name value of the first user?

Selecting the First User's Name

Retrieve the name stored in the first dictionary inside info.

Select the list element: Use info[0] to select the first element of the top-level list.

Select the dictionary value: Use ['name'] on that dictionary to retrieve the value associated with the name key.

Combine the operations: Write both operations together as info[0]['name'].

The expression info[0]['name'] retrieves Chuck.

python
Output
info[0] produces the first dictionary.
info[0]['name'] produces 'Chuck'.
info[1]['name'] selects the name value from the second dictionary.

Reading Native Values

After json.loads() has returned the Python structure, extraction uses standard Python operations. The JSON library is needed to perform the parsing step, but the source explains that no JSON library methods are needed afterward to extract values. You can use the list's index positions, dictionary keys, loops, and ordinary Python methods on the returned data.

selectsselectsinfolist[0]list indexinfo[0]dictionary['name']dictionary keyname valueChuck
Which Python structure is being accessed at each part of the expression?

Suppose a parsed list contains several user dictionaries. To inspect one user, first choose that user's position in the list. To inspect one field, then choose the field's dictionary key. This is the same path represented by info[0]['name']: list position first, dictionary key second.

Common Navigation Mistakes

  • Trying to retrieve the name with only info['name']

    The top-level value info is a list in the example, so the first operation must select a list element.

    Fix: Select the dictionary first with info[0], then use the key: info[0]['name'].

  • Using a JSON-specific extraction method after parsing

    json.loads() has already returned native Python structures.

    Fix: Use standard Python indexing, keys, loops, and methods on the parsed result.

  • Stopping after selecting the dictionary

    info[0] selects the first dictionary but does not yet select one value inside that dictionary.

    Fix: Add the dictionary key, such as info[0]['name'], when the target is a field value.

Read a chained expression from left to right. At each bracket operation, ask what kind of structure you currently have and what the next selector should be. In info[0]['name'], [0] operates on the list and ['name'] operates on the dictionary selected by the first operation.

Practice the Path

EASY

Assume info is the parsed list described in this article. Write the Python expression that retrieves the name value from the first dictionary. Then explain what each bracket operation selects.

Hints
  • The top-level value is a list.
  • Use index 0 to select its first element.
  • The selected element is a dictionary with a name key.

What do you think happens?

What does info[0]['name'] select in the parsed example?

  • The first dictionary
  • The name value Chuck
  • The entire list
  • The second dictionary
Reveal answer

Answer: The name value Chuck

The [0] operation selects the first dictionary from the list, and ['name'] selects that dictionary's name value.

Key Takeaways

  1. json.loads() returns native Python structures rather than JSON text.
  2. In the example, the parsed value info is a list containing dictionaries.
  3. Use a list index such as info[0] to select an element.
  4. Use a dictionary key such as ['name'] to select a value inside a dictionary.
  5. Chain selectors such as info[0]['name'] to navigate from the list to a target value.

Key Takeaways

  • json.loads() changes JSON text into native Python structures.
  • The parsed example is a list whose elements are dictionaries.
  • List indexes and dictionary keys can be chained to reach nested values.
  • After parsing, use ordinary Python operations to extract and work with the data.