Working with JSON files
As programs grow beyond toy examples, they need a way to store and exchange structured data. JSON fills that role for a huge range of systems, from configuration files to web APIs and local data stores. In Python programs that generate or consume data—especially AI-adjacent systems—JSON is often the simplest bridge between code and durable structure.
What JSON is and when it is used
JSON stands for JavaScript Object Notation, but its use is not limited to JavaScript. It is a plain-text format designed to represent structured data in a way that is easy for both humans and programs to read.
JSON is commonly used for configuration files, saved program state, API responses, and small datasets that need structure without complexity.
Representing structured data using dictionaries and lists
In Python, JSON maps directly onto familiar data structures. JSON objects become dictionaries, and JSON arrays become lists.
This close correspondence is what makes JSON practical in Python. Once loaded, JSON data behaves like ordinary dictionaries and lists, and we work with it using standard language features.
Loading JSON data from a file
Python’s standard library includes the json module for working with JSON data. Loading JSON from a file means reading the file and converting its contents into Python data structures.
import json
with open("planets.json", "r") as file:
data = json.load(file)
After this step, data contains dictionaries and lists that mirror the structure of the JSON file.
Accessing values in loaded JSON data
Once JSON data is loaded, we access it the same way we access any dictionary or list. Keys and indexes are used to navigate the structure.
planet_name = data["name"]
moons = data["moons"]
At this point, the JSON file is no longer special. We are simply working with Python objects in memory.
Writing structured data back to a JSON file
Programs often modify structured data and then write it back out. Writing JSON means converting Python dictionaries and lists back into JSON text and saving it to a file.
import json
with open("planets.json", "w") as file:
json.dump(data, file, indent=2)
This produces a readable JSON file that preserves the structure of the data and can be reused by other programs.
Conclusion
We now know what JSON is, how it maps cleanly onto Python dictionaries and lists, and how to load and save structured data using the standard library. That is enough to recognize JSON as a practical tool for storing and exchanging data in real Python programs.