How to Parse JSON in Python
If you have JSON text and want to use it in Python, the usual goal is to turn that text into normal Python data such as dictionaries and lists.
This page shows you how to:
- Parse JSON text into Python objects
- Understand what type Python gives you back
- Read values from parsed JSON safely
- Choose between
json.loads()andjson.load()
Quick answer
import json
json_text = '{"name": "Ana", "age": 25, "is_admin": false}'
data = json.loads(json_text)
print(data)
print(data["name"])
print(data["age"])
Output:
{'name': 'Ana', 'age': 25, 'is_admin': False}
Ana
25
Use json.loads() for a JSON string. Use json.load() when reading JSON from a file object.
What this page helps you do
- Parse JSON text into Python objects
- Understand what Python type you get back
- Read values from parsed JSON safely
- Choose between
json.loads()andjson.load()
What JSON becomes in Python
When Python parses JSON, it converts JSON values into matching Python values.
- JSON object becomes a Python dictionary
- JSON array becomes a Python list
- JSON string becomes a Python
str - JSON number becomes
intorfloat - JSON
trueandfalsebecomeTrueandFalse - JSON
nullbecomesNone
Example:
import json
json_text = '''
{
"user": "Ana",
"age": 25,
"skills": ["Python", "SQL"],
"active": true,
"manager": null
}
'''
data = json.loads(json_text)
print(type(data))
print(type(data["skills"]))
print(data["active"])
print(data["manager"])
Output:
<class 'dict'>
<class 'list'>
True
None
If you want a broader overview, see the Python json module overview.
Parse JSON from a string with json.loads()
Use json.loads() when your JSON is already in a Python string.
Steps:
- Import the built-in
jsonmodule - Pass a JSON-formatted string to
json.loads() - Store the result in a variable
- Access dictionary keys or list items from the result
Example:
import json
json_text = '{"city": "Paris", "population": 2148000}'
data = json.loads(json_text)
print(data)
print(data["city"])
print(data["population"])
Output:
{'city': 'Paris', 'population': 2148000}
Paris
2148000
In this example:
json_textis a stringjson.loads(json_text)parses the string- The result is a Python dictionary
If you want a focused explanation, see json.loads() explained.
Example: parsing a JSON array
JSON does not always become a dictionary. It can also become a list.
import json
json_text = '["apple", "banana", "orange"]'
data = json.loads(json_text)
print(data)
print(type(data))
print(data[0])
Output:
['apple', 'banana', 'orange']
<class 'list'>
apple
This matters because you access a list with indexes, not dictionary keys.
Parse JSON from a file with json.load()
Use json.load() when your JSON is in a file.
Steps:
- Open the file with
open() - Pass the file object to
json.load() - Use a
withstatement so the file closes automatically - This works well for local
.jsonfiles
Example file, data.json:
{
"name": "Ana",
"age": 25,
"languages": ["Python", "JavaScript"]
}
Python code:
import json
with open("data.json", "r", encoding="utf-8") as file:
data = json.load(file)
print(data)
print(data["name"])
print(data["languages"])
Output:
{'name': 'Ana', 'age': 25, 'languages': ['Python', 'JavaScript']}
Ana
['Python', 'JavaScript']
In this example:
open()opens the filejson.load(file)reads and parses the JSON from that file- The result is again normal Python data
For more detail, see json.load() explained.
How to access parsed JSON data
After parsing, work with the result based on its Python type.
- Use square brackets for known dictionary keys
- Use
.get()when a key might be missing - Use indexes for lists
- Check the data type if you are not sure what was parsed
Access dictionary values
import json
json_text = '{"name": "Ana", "age": 25}'
data = json.loads(json_text)
print(data["name"])
print(data.get("age"))
print(data.get("email"))
Output:
Ana
25
None
Why use .get()?
data["age"]works if the key existsdata.get("email")returnsNoneif the key is missing- This helps you avoid some errors when data is incomplete
If you keep getting missing-key problems, see how to fix KeyError when accessing dictionary values.
Access list items
import json
json_text = '["red", "green", "blue"]'
data = json.loads(json_text)
print(data[0])
print(data[1])
Output:
red
green
Check the parsed type first
If you are not sure whether the parsed result is a dictionary or a list, inspect it first.
import json
json_text = '[{"name": "Ana"}, {"name": "Ben"}]'
data = json.loads(json_text)
print(type(data))
print(data)
Output:
<class 'list'>
[{'name': 'Ana'}, {'name': 'Ben'}]
This tells you that data is a list, so you need an index first:
print(data[0]["name"])
Common parsing errors and what they mean
A few small mistakes can stop JSON from parsing.
Common problems:
- Invalid JSON format causes a decoding error
- Single quotes are not valid in JSON strings
- Trailing commas can break parsing
- Trying to parse plain Python dict text is not the same as JSON
- A missing key after parsing can cause
KeyError
Example: single quotes are not valid JSON
This is not valid JSON:
import json
json_text = "{'name': 'Ana'}"
data = json.loads(json_text)
This will raise a decoding error because JSON requires double quotes around keys and string values.
Correct version:
import json
json_text = '{"name": "Ana"}'
data = json.loads(json_text)
print(data)
Example: trailing comma
This is also invalid JSON:
import json
json_text = '{"name": "Ana",}'
data = json.loads(json_text)
The comma before } makes the JSON invalid.
Correct version:
import json
json_text = '{"name": "Ana"}'
data = json.loads(json_text)
print(data)
Example: Python dictionary text is not always JSON
Beginners sometimes confuse Python dictionary syntax with JSON syntax.
Python dict:
{'name': 'Ana', 'active': True}
JSON text:
{"name": "Ana", "active": true}
Key differences:
- JSON uses double quotes
- JSON uses
true,false, andnull - Python uses
True,False, andNone
If your JSON is invalid, see how to fix JSONDecodeError for invalid JSON.
Simple debugging steps
If parsing fails, try these steps:
- Print the raw JSON before parsing
- Check that keys and string values use double quotes
- Confirm whether your input is a string or a file
- Use
type()to inspect the parsed result - Start with a small JSON example if parsing fails
Useful commands:
print(json_text)
print(type(data))
print(data)
print(data.keys())
You can also validate a JSON file from the command line:
python -m json.tool data.json
That command helps you check whether a file contains valid JSON.
Common mistakes
These are some very common beginner mistakes:
- Using
json.load()on a string instead of a file object - Using
json.loads()on a file object instead of file content - Using single quotes in JSON text
- Forgetting to import
json - Assuming parsed data is always a dictionary
- Accessing keys that do not exist
Example of mixing up load() and loads():
import json
json_text = '{"name": "Ana"}'
# Correct for a string
data = json.loads(json_text)
print(data["name"])
Remember:
load= fileloads= string
A simple way to remember it is that loads() means “load string”.
FAQ
What is the difference between json.load() and json.loads()?
json.load() reads JSON from a file object. json.loads() reads JSON from a string.
Does parsing JSON return a dictionary every time?
No. It can return a dictionary, list, string, number, boolean, or None depending on the JSON.
Why does my JSON fail to parse?
Common reasons are single quotes, trailing commas, or invalid formatting.
How do I get a value from parsed JSON safely?
If the result is a dictionary, use .get() for keys that may be missing.
See also
- Python json module overview
json.loads()explainedjson.load()explained- How to convert a dictionary to JSON in Python
- How to pretty-print JSON in Python
- Fix
KeyErrorwhen accessing dictionary values - Fix
JSONDecodeErrorfor invalid JSON
Once you are comfortable parsing JSON, the next useful step is working with JSON files and API responses.