json.loads() Function Explained
json.loads() converts JSON text into normal Python data.
Use it when your JSON is already stored in a string, such as:
- API response text
- JSON copied into a variable
- JSON read with
file.read()
It is part of Python’s built-in json module.
Quick example #
import json
text = '{"name": "Ana", "age": 20, "is_student": true}'
data = json.loads(text)
print(data)
print(type(data))
print(data["name"])
Output:
{'name': 'Ana', 'age': 20, 'is_student': True}
<class 'dict'>
Ana
Use json.loads() when your JSON is already in a string. It returns normal Python objects like dict, list, str, int, float, bool, and None.
What json.loads() does #
json.loads():
- Parses a JSON-formatted string
- Converts JSON text into Python objects
- Commonly returns a
dictorlist - Can also return
str,int,float,bool, orNone - Comes from Python’s built-in
jsonmodule
In short, it takes text like this:
'{"name": "Ana", "age": 20}'
and turns it into a real Python dictionary:
{"name": "Ana", "age": 20}
When to use json.loads() #
Use json.loads() when the input is a string containing valid JSON.
Common cases:
- JSON returned from an API as text
- A text file that you already read into a string
- JSON pasted into your code for testing
- A JSON list stored in a variable
Do not use it directly on a file object.
If you are reading JSON from an open file, use json.load() instead.
Basic example #
Here is a simple example that converts a JSON object string into a Python dictionary:
import json
text = '{"name": "Maya", "age": 18, "is_student": true}'
data = json.loads(text)
print(data)
print(type(data))
print(data["is_student"])
print(data["name"])
Output:
{'name': 'Maya', 'age': 18, 'is_student': True}
<class 'dict'>
True
Maya
Notice this important detail:
- JSON
truebecomes PythonTrue
After parsing, you can use normal Python dictionary access:
print(data["name"])
What types JSON becomes in Python #
When json.loads() parses JSON, it converts each JSON type into a matching Python type.
| JSON type | Python type |
|---|---|
| object | dict |
| array | list |
| string | str |
| number | int or float |
| true / false | True / False |
| null | None |
Example:
import json
text = '''
{
"user": "Liam",
"score": 9.5,
"active": true,
"tags": ["python", "json"],
"extra": null
}
'''
data = json.loads(text)
print(type(data))
print(type(data["user"]))
print(type(data["score"]))
print(type(data["active"]))
print(type(data["tags"]))
print(data["extra"])
Output:
<class 'dict'>
<class 'str'>
<class 'float'>
<class 'bool'>
<class 'list'>
None
json.loads() vs json.load() #
These two functions are very similar, but they read from different kinds of input.
json.loads() #
Use json.loads() when you have a string:
import json
text = '{"name": "Ana"}'
data = json.loads(text)
print(data)
json.load() #
Use json.load() when you have a file object:
import json
with open("data.json", "r") as file:
data = json.load(file)
print(data)
The difference is:
json.loads()reads from a stringjson.load()reads from a file object
If you want the file-based version, see json.load() explained.
Common errors beginners hit #
Here are some common problems when using json.loads().
Passing invalid JSON text #
This causes a json.JSONDecodeError.
Example:
import json
text = "{name: 'Ana'}"
data = json.loads(text)
This fails because the text is not valid JSON.
Using single quotes instead of double quotes #
JSON requires double quotes for:
- object keys
- string values
Invalid JSON:
"{'name': 'Ana'}"
Valid JSON:
'{"name": "Ana"}'
Forgetting to import json #
This causes a NameError.
text = '{"name": "Ana"}'
data = json.loads(text)
Fix:
import json
Passing a dict instead of a JSON string #
If you already have a Python dictionary, you do not need json.loads().
Wrong:
import json
data = {"name": "Ana"}
result = json.loads(data)
Correct:
data = {"name": "Ana"}
print(data["name"])
Use json.dumps() if you want to convert a Python object into JSON text.
Trying to parse an empty string #
An empty string is not valid JSON.
import json
text = ""
data = json.loads(text)
This raises json.JSONDecodeError.
How to handle bad JSON safely #
If the JSON might be invalid, use try and except.
import json
text = '{"name": "Ana", "age": 20}'
try:
data = json.loads(text)
print("Parsed successfully:", data)
except json.JSONDecodeError:
print("Invalid JSON")
print("Raw text:", repr(text))
This helps your program fail safely instead of crashing.
When parsing fails:
- Print the raw string
- Check whether the input is really a string
- Look for missing commas
- Check for single quotes instead of double quotes
- Look for extra characters before or after the JSON
Helpful debugging lines:
print(text)
print(type(text))
print(repr(text))
Example with bad JSON:
import json
text = "{'name': 'Ana'}"
try:
data = json.loads(text)
except json.JSONDecodeError as error:
print("JSON error:", error)
print("Problem text:", repr(text))
Common real-world uses #
json.loads() is often used in practical Python code.
Parsing API response text #
Some APIs return JSON as text. You can parse that text into Python data.
import json
response_text = '{"status": "ok", "results": [1, 2, 3]}'
data = json.loads(response_text)
print(data["status"])
print(data["results"])
Converting copied JSON text into a Python dict #
import json
text = '{"city": "Tokyo", "population": 14}'
data = json.loads(text)
print(data["city"])
Reading JSON after using file.read() #
If you already used .read(), the result is a string, so json.loads() is correct.
import json
with open("data.json", "r") as file:
text = file.read()
data = json.loads(text)
print(data)
If you want to parse directly from the file object, use json.load().
Turning a JSON list string into a Python list #
import json
text = '["apple", "banana", "cherry"]'
items = json.loads(text)
print(items)
print(type(items))
print(items[0])
Output:
['apple', 'banana', 'cherry']
<class 'list'>
apple
Common mistakes #
These are frequent causes of problems:
- JSON uses double quotes, but your input uses single quotes
- The input is already a Python
dictorlist - The string is empty or incomplete
- The JSON text has a trailing comma
- You used
json.loads()on a file object instead ofjson.load()
If you are not sure what you have, inspect it first:
print(text)
print(type(text))
print(repr(text))
FAQ #
What does json.loads() return? #
It returns a Python object created from the JSON string, usually a dict or list.
What is the difference between json.loads() and json.load()? #
json.loads() parses a string. json.load() parses a file object.
Can json.loads() parse single quotes? #
No. Valid JSON uses double quotes for strings and object keys.
Why do I get JSONDecodeError? #
The string is not valid JSON. Check quotes, commas, brackets, and extra characters.
Do I need json.loads() if I already have a Python dict? #
No. json.loads() is only for JSON strings, not Python objects.
See also #
- Python
jsonmodule overview json.load()function explainedjson.dumps()function explained- How to parse JSON in Python
- How to handle API responses in Python
If you are choosing the right JSON tool:
- Use
json.loads()to parse JSON from a string - Use
json.load()to parse JSON from a file - Use
json.dumps()to convert Python objects back into JSON text