Reading JSON in Python with the built-in module
Python ships with everything needed for the common case: no extra package is required to open a JSON file and work with its contents as native Python objects.
The choice depends on where the JSON lives — on disk, or already in a string.
Reading a JSON file step by step
Start by importing json, then open the file in read mode inside a with block so the handle closes automatically. Passing that handle to json.load returns a dictionary for a JSON object. From there, ordinary key access retrieves individual values, including nested objects under their own keys. The worked example in module code0001 shows a person record with a nested address, and module code0002 reads it and prints the name field, yielding John Doe.
File versus string: load or loads
If the JSON is not in a file but already in a string variable, skip the file handling entirely. json.loads accepts that string directly and produces the same Python dictionary. A quick decision check: file on disk — open it and use json.load; text in memory — call json.loads on the string; need to inspect the result — treat it as a dictionary and index by key, remembering that nested objects become nested dictionaries.
Going further
For the full API reference, consult the official Python JSON documentation. Related questions cover parsing JSON in JavaScript, how a JSON parser works internally, and what a JSON parser is, while a longer guide walks through reading and parsing JSON data with Python in more depth.
| Situation | Method | Result |
|---|---|---|
| JSON stored in a file on disk | json.load with an open file handle | Python dictionary |
| JSON already held as a string | json.loads on the string | Python dictionary |
Both methods are part of Python's built-in json module; no third-party package is needed.
Frequently asked questions
When should I use json.load instead of json.loads?
Use json.load when the content is already in a file you open in Python. Use json.loads when the JSON is already held as a string in memory, for example after receiving it as text. Both return Python data structures such as dictionaries that you can index by key.
What does a JSON parser do?
A JSON parser is the component that turns JSON text into a usable data structure in a programming language. In Python, the built-in json module performs that role for both files and strings.
{
"name": "John Doe",
"age": 32,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
},
}