Understanding JSON Structure and Syntax
Parsing and navigating are separate actions. json.loads() performs the translation. Python indexing, dictionary keys, and for loops perform the navigation afterward.
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
A JSON string is text that describes data. The important transition happens when json.loads() parses that string: the result is represented as native Python lists and dictionaries. After this translation, you work with the resulting structures using ordinary Python syntax.
The Parsing Boundary
json.loads() is the boundary between JSON text and Python data structures. Before parsing, the data is a JSON string. After parsing, the data is represented as lists and dictionaries that Python can access directly. The result is not a special JSON type that needs its own navigation methods.
Parsing and navigating are separate actions. json.loads() performs the translation. Python indexing, dictionary keys, and for loops perform the navigation afterward.
A parsed collection
Suppose a JSON string describes a collection of records. After json.loads() processes it, what kind of Python structure should you expect to navigate?
- 1Parse
Pass the JSON string to
json.loads(). - 2Identify the result
The parsed result is represented using native Python lists and dictionaries.
- 3Navigate
Use list indexing to select an element and dictionary keys to select a value inside that element.
The parsed data can be traversed with standard Python syntax rather than special JSON methods.
Nested Data Paths
Parsed data can contain lists and dictionaries nested within one another. To reach a value, follow the structure one level at a time. First use a list index when the current value is a list. Then use a dictionary key when the current value is a dictionary. Continue chaining these operations until you reach the desired value.
The source pattern info[0]['name'] demonstrates this path. The index operation selects the first element of info. The key access then selects the value associated with the name key in that element.
A chained access expression mirrors the nesting order: list index first, dictionary key next, and any further index or key access after that.
Following an Access Chain
Read an access chain from left to right. Start with the parsed variable. If it represents a list, apply a numeric index. The selected element may be a dictionary; if so, apply a key to select one of its values. This is ordinary Python traversal of the parsed result.
If a parsed value is stored in info and info is a list whose first element is a dictionary, info[0]['name'] reaches the name value in that first dictionary.
Mistakes with Parsed Data
Common mistake Treating the parsed result as a special JSON type
What it looks likeLooking for special JSON methods to move through the result
Why it failsjson.loads()translates the JSON string into native Python lists and dictionaries.Do this insteadUse standard Python list indexing, dictionary keys, and for loops after parsing.
Common mistake Ignoring the nesting order
What it looks likeTrying to use a dictionary key before selecting an element from a surrounding list
Why it failsEach access operation must match the kind of structure currently being accessed.
Do this insteadFollow the structure from the outside inward: select a list element, then select a dictionary key when appropriate.
Common mistake Forgetting that a chained access follows multiple levels
What it looks likeReading info[0]['name'] as one indivisible operation
Why it failsThe expression contains two navigation steps: index 0 and key name.
Do this insteadInterpret the chain one operation at a time.
When an access expression is confusing, identify the current structure at each step. Ask whether the next operation is selecting a list element by index or selecting a dictionary value by key.
Practice the Translation
Imagine that json.loads() has produced a value named records. The value records is a list, and its first element is a dictionary containing the key name. Describe the sequence of standard Python operations needed to reach that name value.
Hints
- Begin with the list variable.
- Use index 0 to select its first element.
- Use the dictionary key name to select the value inside that element.
What do you think happens?
What does the access path info[0]['name'] do first?
Reveal answer
It selects the element at list index 0.
The expression follows the nesting order. It first indexes the list, then uses the name key on the selected dictionary.
Key Takeaways
- 1
json.loads()translates a JSON string into native Python lists and dictionaries. - 2The parsed result is not a special JSON type.
- 3Use list indexes to select elements and dictionary keys to select values.
- 4Chained access follows the nesting order of the parsed data.
- 5After parsing, standard Python syntax is enough to traverse the data, including list indexing, dictionary keys, and for loops.
At a glance
json.loads()performs the conversion from JSON text to native Python lists and dictionaries.- Parsing is different from navigation: after parsing, use standard Python syntax.
- Nested data is accessed by chaining list indexes and dictionary keys in structural order.
- The access path info[0]['name'] selects the first list element and then its name value.
- Special JSON traversal methods are unnecessary after the conversion.