Python JSON Parser Example
This beginner-friendly example shows how to parse JSON in Python and turn it into normal Python data.
You will learn:
- how to import the
jsonmodule - how to parse a JSON string with
json.loads() - how JSON becomes Python data types
- how to read values from the parsed result safely
If you want the shortest working example first, start here:
import json
json_text = '{"name": "Alice", "age": 25, "skills": ["Python", "SQL"]}'
data = json.loads(json_text)
print(data)
print(data["name"])
print(data["skills"][0])
Note: Use json.loads() for a JSON string. Use json.load() when reading JSON from a file.
What this example shows #
In this page, the script will show you how to:
- import Python’s built-in
jsonmodule - parse JSON text with
json.loads() - understand what Python data type you get back
- access values from dictionaries, lists, and nested data
This is a common pattern when working with:
- API responses
- configuration files
- saved app data
- practice projects that use structured data
Example input JSON #
Here is a small JSON string. It includes:
- a string
- a number
- a boolean
- a list
- a nested object
json_text = """
{
"name": "Alice",
"age": 25,
"active": true,
"skills": ["Python", "SQL"],
"address": {
"city": "London"
}
}
"""
A few important things to notice:
- JSON uses double quotes around keys and string values.
truein JSON is lowercase.- This is still just text until Python parses it.
Parse JSON with json.loads() #
Use json.loads() when your JSON is stored in a string.
import json
json_text = """
{
"name": "Alice",
"age": 25,
"active": true,
"skills": ["Python", "SQL"],
"address": {
"city": "London"
}
}
"""
data = json.loads(json_text)
print(data)
print(type(data))
What this does #
The key line is:
data = json.loads(json_text)
This converts the JSON text into Python data.
In this example, the top-level JSON value is an object, so the result becomes a Python dictionary.
Expected output:
{'name': 'Alice', 'age': 25, 'active': True, 'skills': ['Python', 'SQL'], 'address': {'city': 'London'}}
<class 'dict'>
If the JSON started with [ and ended with ], the result would usually be a Python list instead.
For a deeper explanation of this function, see json.loads() explained.
Read values from parsed JSON #
Once the JSON is parsed, you can use normal Python indexing to read values.
import json
json_text = """
{
"name": "Alice",
"age": 25,
"active": true,
"skills": ["Python", "SQL"],
"address": {
"city": "London"
}
}
"""
data = json.loads(json_text)
print(data["name"])
print(data["skills"][0])
print(data["address"]["city"])
Expected output:
Alice
Python
London
How each line works #
data["name"]gets a top-level value from the dictionarydata["skills"][0]gets the first item from theskillslistdata["address"]["city"]gets a nested value step by step
If you try to access a key that does not exist, Python raises a KeyError. See how to fix KeyError when accessing dictionary values.
Safer access with get() #
If you are not sure a key exists, get() can be safer than direct indexing.
import json
json_text = '{"name": "Alice"}'
data = json.loads(json_text)
print(data.get("name"))
print(data.get("email"))
print(data.get("email", "Not provided"))
Expected output:
Alice
None
Not provided
This helps avoid errors when some fields may be missing.
JSON types and Python types #
When Python parses JSON, the data types change like this:
- JSON object → Python
dict - JSON array → Python
list - JSON string → Python
str - JSON number → Python
intorfloat - JSON
true/false→ PythonTrue/False - JSON
null→ PythonNone
Example:
import json
json_text = """
{
"title": "Book",
"price": 19.99,
"in_stock": true,
"tags": ["fiction", "popular"],
"discount": null
}
"""
data = json.loads(json_text)
print(type(data))
print(type(data["title"]))
print(type(data["price"]))
print(type(data["in_stock"]))
print(type(data["tags"]))
print(data["discount"])
Expected output:
<class 'dict'>
<class 'str'>
<class 'float'>
<class 'bool'>
<class 'list'>
None
Parse JSON from a file #
Use json.load() when the JSON is in a file.
import json
with open("data.json", "r") as file:
data = json.load(file)
print(data)
load() vs loads() #
json.load(file)reads JSON from a file objectjson.loads(text)reads JSON from a string
That is the main difference.
If you want a full guide, see json.load() explained and how to parse JSON in Python.
Common errors beginners make #
Here are some common problems when parsing JSON.
1. Using single quotes inside JSON text #
This is invalid JSON:
json_text = "{'name': 'Alice'}"
JSON requires double quotes:
json_text = '{"name": "Alice"}'
If your JSON syntax is wrong, Python may raise a decoding error. See how to fix JSONDecodeError for invalid JSON.
2. Confusing json.load() and json.loads() #
This is a common mix-up:
load= fileloads= string
A simple memory trick:
- load = load from file
- loads = load from string
3. Trying to access a missing key #
This causes an error:
import json
data = json.loads('{"name": "Alice"}')
print(data["email"])
Use get() if the key may not exist:
print(data.get("email"))
4. Forgetting that a parsed array is a list #
If the JSON starts as an array:
import json
data = json.loads('["Python", "SQL", "Git"]')
print(type(data))
print(data[0])
Expected output:
<class 'list'>
Python
You cannot use a string key like data["name"] on a list.
When to use this pattern #
This simple parsing pattern is useful when you are:
- reading API responses
- loading app settings from JSON
- working with saved data files
- practicing how structured text becomes Python objects
If you plan to use JSON from web requests, you may also like how to handle API responses in Python.
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 parsed JSON always become a dictionary? #
No. A JSON object becomes a dict, but a JSON array becomes a list.
Why does my JSON string fail to parse? #
It may use invalid JSON syntax, such as single quotes, missing commas, or trailing commas.
How do I get a nested value from parsed JSON? #
Use normal Python indexing step by step, such as data['user']['name'].