Iterating Over Parsed Data Structures
json.loads() returns native Python data structures (lists and dictionaries), not JSON text.
Try it: Parsing JSON into Python
How json.loads() reads JSON text token by token and builds Python dicts, lists, strings, numbers, True/False and None, how an access path like data["users"][0]["name"] walks that structure, and what json.dumps() writes back.
How it works
- The scanner reads the next token: a brace or bracket, a string in double quotes, a number, true / false / null, or a ',' / ':' delimiter.
- '{' starts a Python dict and '[' a list; both are pushed on a parse stack until their closing '}' or ']'.
- Inside a dict every key must be a double-quoted string followed by ':' and a value; items are separated by ','.
- Anything else stops the parse with JSONDecodeError, reporting the message, line, column and character position.
- The access path is applied one subscript at a time: dicts take keys (KeyError if missing), lists take integer indexes (IndexError if out of range).
- json.dumps() turns the Python objects back into JSON text (True → true, None → null), optionally indented.
Default run (43 steps): json.loads() receives 105 characters of text. The scanner starts at char 0 and reads one token at a time. … json.dumps(data, indent=2) turns the Python objects back into JSON text (171 characters): True → true, None → null, and strings get double quotes.
Simplified: Written from scratch to match CPython 3.12's json module (messages and positions checked against it); input is limited to 400 characters and 24 nesting levels, and the access path accepts only ["key"] and [index] subscripts. Python 3.13 reports a trailing comma in an object with a different message.
Loading the simulation…
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.
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.
Selecting the First User's Name
Retrieve the name stored in the first dictionary inside info.
- 1Select the list element
Use info[0] to select the first element of the top-level list.
- 2Select the dictionary value
Use ['name'] on that dictionary to retrieve the value associated with the name key.
- 3Combine the operations
Write both operations together as info[0]['name'].
The expression info[0]['name'] retrieves Chuck.
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.
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
Common mistake Trying to retrieve the name with only info['name']
What it looks likeinfo['name']Why it failsThe top-level value info is a list in the example, so the first operation must select a list element.
Do this insteadSelect the dictionary first with info[0], then use the key: info[0]['name'].
Common mistake Using a JSON-specific extraction method after parsing
What it looks likeTreating info as though it were still raw JSON text
Why it failsjson.loads()has already returned native Python structures.Do this insteadUse standard Python indexing, keys, loops, and methods on the parsed result.
Common mistake Stopping after selecting the dictionary
What it looks likeinfo[0]Why it failsinfo[0] selects the first dictionary but does not yet select one value inside that dictionary.
Do this insteadAdd 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
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?
Reveal 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. - 2In the example, the parsed value info is a list containing dictionaries.
- 3Use a list index such as info[0] to select an element.
- 4Use a dictionary key such as ['name'] to select a value inside a dictionary.
- 5Chain selectors such as info[0]['name'] to navigate from the list to a target value.
At a glance
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.