The Nested Record
Hoppy slides open the next drawer in the data cabinet. This time the record is not flat anymore. Inside the drawer there is a smaller compartment, inside that there is a hidden note, and beside the note there is a short list of clue tokens.
So today's job is not to learn a giant new system. It is to build one practical instinct: real records often need to be read one layer at a time before you reach the value you actually want.
Some values are themselves a smaller record or a list
When a JSON field contains another dict or a list, you are looking at a nested structure. That does not have to feel scary. Most of the time, you simply step into one layer, then step into the next layer.
camp_box = {
"outer": {
"label": "Moss Drawer",
"tokens": ["leaf", "bell", "rope"]
}
}
label = camp_box["outer"]["label"]
first_token = camp_box["outer"]["tokens"][0]
print("Label:", label)
print("First token:", first_token)
The reading path is: first go to "outer", then find "label" or "tokens" inside it. If tokens is a list, then you read one item from that list with an index, just like before.
Today's task: open the drawers layer by layer and pull out three clues
The real starter already loads nested_record.json into nested_record. Your job is not to rewrite the file-loading step. Your job is to follow the record's path and extract the three target values.
The starter prints nested_record before anything else. Take one moment to notice which part is the big drawer, which part is the hidden note, and which part is the list.
Set drawer_label to the drawer label inside cabinet. Set clue_word to the clue inside hidden_note. Set second_token to the second element from the tokens list.
When you run the code, you will see the full record first and the three extracted values after it. That layer-by-layer movement is the most common first way to read nested JSON.
Today is only about light nested reading: nested dict, nested list, and layer-by-layer access. We are not expanding into deep complex data, recursion, or error handling as the main topic.
Suggested SolutionExpandCollapse
import json
with open("nested_record.json", "r", encoding="utf-8") as file:
nested_record = json.load(file)
print("Nested record:", nested_record)
drawer_label = nested_record["cabinet"]["drawer_label"]
clue_word = nested_record["cabinet"]["hidden_note"]["clue_word"]
second_token = nested_record["cabinet"]["hidden_note"]["tokens"][1]
print("Drawer label:", drawer_label)
print("Clue word:", clue_word)
print("Second token:", second_token)Advanced TipsWant more? Click to expandClick to collapse
The main feeling to keep from this lesson is that a nested record is not mysterious. It simply means there is another layer inside the current value. If you follow the path calmly, you can pull out the exact information you need.
In the next lesson, Hoppy will take one more step: not just reading someone else's structured record, but building a JSON-style result of its own.