0
0
Pythonprogramming~10 mins

Working with JSON files in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Working with JSON files
Open JSON file in read mode
Read file content as string
Parse string to Python object (dict/list)
Use or modify Python object
Open JSON file in write mode
Convert Python object to JSON string
Write JSON string to file
Close file
This flow shows how Python reads JSON data from a file into Python objects, modifies them, then writes back as JSON.
Execution Sample
Python
import json

with open('data.json', 'r') as f:
    data = json.load(f)

print(data['name'])
This code reads JSON from 'data.json' file and prints the value of the 'name' key.
Execution Table
StepActionCode LineResult/Value
1Open 'data.json' file in read modewith open('data.json', 'r') as f:File object 'f' opened
2Read JSON content and parse to Python dictdata = json.load(f){'name': 'Alice', 'age': 30}
3Access 'name' key in dictprint(data['name'])Alice
4File 'data.json' automatically closedEnd of with blockFile closed
💡 File is closed after reading; program prints 'Alice' and ends.
Variable Tracker
VariableStartAfter json.loadFinal
fNoneFile object openedFile closed
dataNone{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 30}
Key Moments - 3 Insights
Why do we use 'with open(...) as f' instead of just open()?
Using 'with' ensures the file is properly closed after reading, even if errors occur, as shown in step 4 of the execution_table.
What type is 'data' after json.load(f)?
'data' becomes a Python dictionary representing the JSON object, as seen in step 2 where data = {'name': 'Alice', 'age': 30}.
What happens if the JSON file is empty or malformed?
json.load(f) will raise an error and stop execution; this is why proper JSON format is required before loading.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'data' after step 2?
ANone
BFile object
C{'name': 'Alice', 'age': 30}
D'Alice'
💡 Hint
Check the 'Result/Value' column in row with Step 2 in execution_table.
At which step is the file closed?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look at the 'Action' column in execution_table for file closing.
If the JSON file contained a list instead of a dict, what would 'data' be after json.load(f)?
AA string
BA Python list
CA file object
DAn integer
💡 Hint
json.load converts JSON arrays to Python lists, similar to how it converts JSON objects to dicts.
Concept Snapshot
Working with JSON files in Python:
- Use 'import json' module
- Open file with 'with open(filename, mode) as f'
- Read JSON with 'json.load(f)' to get Python dict/list
- Modify Python object as needed
- Write back with 'json.dump(obj, f)'
- 'with' block auto-closes file
- JSON maps to Python types (object->dict, array->list)
Full Transcript
This lesson shows how Python reads and writes JSON files. First, the program opens a JSON file in read mode using a 'with' statement, which safely manages the file. Then it reads the file content and parses it into a Python dictionary using json.load. The program accesses data from this dictionary, for example printing the value of the 'name' key. After the 'with' block ends, the file is automatically closed. This process allows Python programs to work with JSON data easily by converting it to familiar Python objects. Writing JSON back to a file uses a similar process with json.dump. Understanding these steps helps beginners handle JSON files safely and effectively.