Concepts / Understanding JSON Structure and Syntax

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.

  • Programming
Interactive lab

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

  1. The scanner reads the next token: a brace or bracket, a string in double quotes, a number, true / false / null, or a ',' / ':' delimiter.
  2. '{' starts a Python dict and '[' a list; both are pushed on a parse stack until their closing '}' or ']'.
  3. Inside a dict every key must be a double-quoted string followed by ':' and a value; items are separated by ','.
  4. Anything else stops the parse with JSONDecodeError, reporting the message, line, column and character position.
  5. 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).
  6. 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.

Educational simulation

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.

JSON stringtextPython listnative structurejson.loads()parsePython dictionarynative structurepass tobecomescan produce
What changes when a JSON string is passed to json.loads()?

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.

Key point

Parsing and navigating are separate actions. json.loads() performs the translation. Python indexing, dictionary keys, and for loops perform the navigation afterward.

Worked example

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?

  1. 1
    Parse

    Pass the JSON string to json.loads().

  2. 2
    Identify the result

    The parsed result is represented using native Python lists and dictionaries.

  3. 3
    Navigate

    Use list indexing to select an element and dictionary keys to select a value inside that element.

Result

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.

infolistfirst elementdictionarynamevalueindex 0 containskey name contains
What contains what inside parsed data?

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.

Key point

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

infolistinfo[0]first elementinfo[0]['name']name valueindex 0key name
How does each index or key move through the structure to reach a value?

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.

In the real world

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 like

    Looking for special JSON methods to move through the result

    Why it fails

    json.loads() translates the JSON string into native Python lists and dictionaries.

    Do this instead

    Use standard Python list indexing, dictionary keys, and for loops after parsing.

  • Common mistake

    Ignoring the nesting order

    What it looks like

    Trying to use a dictionary key before selecting an element from a surrounding list

    Why it fails

    Each access operation must match the kind of structure currently being accessed.

    Do this instead

    Follow 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 like

    Reading info[0]['name'] as one indivisible operation

    Why it fails

    The expression contains two navigation steps: index 0 and key name.

    Do this instead

    Interpret the chain one operation at a time.

Best practice

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

EASY

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?

  • It selects the name key immediately

  • It selects the element at list index 0

  • It calls a special JSON navigation method

Reveal answer
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. 1
    json.loads() translates a JSON string into native Python lists and dictionaries.
  2. 2
    The parsed result is not a special JSON type.
  3. 3
    Use list indexes to select elements and dictionary keys to select values.
  4. 4
    Chained access follows the nesting order of the parsed data.
  5. 5
    After 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.

Related concepts

More concepts starting with U