If you encounter the error “AttributeError: ‘str’ object has no attribute ‘loads'” in Python and don’t know how to solve it, you can refer to this article. Hope you will read this article in its entirety.
What causes the “AttributeError: ‘str’ object has no attribute ‘loads'” error?
Json is a standard format for exchanging data. Usually, Json is in string or text form. Json is similar to the dictionary data type in Python. In Python, a Json library supports converting Python objects to Json and vice versa.
The json.loads method is called from the json library, which parses a Json string and converts it to a dictionary data type in Python.
The error happens because you call loads() on a string.
Example:
import json # The Json string jsonString = '{ "Key1": "Value1", "Key2": "Value2" }' # Decode Json string using json.loads function deCode = jsonString.loads(jsonString) print('The decoded Json string is:', deCode)
Output:
Traceback (most recent call last):
File "./prog.py", line 7, in <module>
deCode = jsonString.loads(jsonString)
AttributeError: 'str' object has no attribute 'loads'
Another cause of the error is overwriting the json module to a string and calling the loads method on that string.
Example:
import json # The Json string json = '{ "Key1": "Value1", "Key2": "Value2" }' # Doing an overwrite, causing an error deCode = json.loads(jsonString) print('The decoded Json string is:', deCode )
Output:
Traceback (most recent call last):
File "./prog.py", line 7, in <module>
deCode = json.loads(jsonString)
AttributeError: 'str' object has no attribute 'loads'
How to solve this error?
Method loads must be called on the module
The cause of the error is that if you call loads on a string to fix it, the loads method needs to be called on the json module.
Example:
- Import the json module.
- Create a Json string.
- Call the loads method on the json module ( json.loads, not jsonString.loads).
import json # a Json string. jsonString = '{ "Key1": "Value1", "Key2": "Value2" }' # Decode Json string using json.loads function. deCode = json.loads(jsonString) print('The decoded Json string is:', deCode)
Output:
The decoded Json string is: {'Key1': 'Value1', 'Key2': 'Value2'}
Import specific functionality from the module
Example:
- You can specifically import the loads method from the json module and use it.
from json import loads # a Json string. jsonString = '{ "Key1": "Value1", "Key2": "Value2" }' # Decode Json string using json.loads function. deCode = loads(jsonString) print('The decoded Json string is:', deCode)
Output:
The decoded Json string is: {'Key1': 'Value1', 'Key2': 'Value2'}
Summary
The article is quite long but I hope you have an idea to fix the AttributeError: ‘str’ object has no attribute ‘loads’ in Python. Method 2: Import specific functionality from the module is quite simpler, so we recommend using it. Leave a comment so I can know how you feel about the article.
Maybe you are interested:
- AttributeError module ‘jwt’ has no attribute ‘encode’ in Python
- AttributeError: ‘str’ object has no attribute ‘items’
- AttributeError: ‘str’ object has no attribute ‘get’ (Python)

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java