Select Page

How to Solve Python AttributeError: ‘str’ object has no attribute ‘loads’

by | Programming, Python, Tips

This error results from trying to call the json.loads() method on a string object. This error typically occurs when you assign a string object to the variable name json, which overrides the json module. You can solve this error by not using reserved names for modules, classes, and methods that you want to use in your program.

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘str’ object has no attribute ‘loads’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘str’ object has no attribute ‘loads’” tells us that the string object does not have the attribute loads().

The loads() method belongs to the json module and deserializes a str, bytes, or bytearray instance containing a JSON document into a Python object.

We can check an object’s attributes by using the built-in dir() method. The dir() method returns all the properties and methods of the specified object as a list.

Let’s verify that loads() is not a str method by using the in operator to check if the method exists in the list object returned by dir().

string = "test"

attributes = dir(string)

print("loads" in attributes)
False

The membership operation returns False.

Let’s prove that loads() is a json method by using the in operator:

import json

attributes = dir(json)

print("loads" in attributes)
True

The membership operation returns True.

Example

Let’s look at an example of how the error can occur using the json module. First, we will import json and define a JSON string.

import json

json =  '{ "name":"Will", "age":45, "city":"Los Angeles"}'

my_dict = json.loads(json)

print(my_dict)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [10], in <cell line: 5>()
      1 import json
      3 json =  '{ "name":"Will", "age":45, "city":"Los Angeles"}'
----> 5 my_dict = json.loads(json)
      7 print(my_dict)

AttributeError: 'str' object has no attribute 'loads'

The error occurs because we named the string json, which overrides the reserved name for the json module. Therefore, when we try to call the loads() method from the json module we are instead trying to call the loads() method on the string object.

Solution

We can solve this error by giving the string object other than json. Let’s look at the revised code:

import json

my_str =  '{ "name":"Will", "age":45, "city":"Los Angeles"}'

my_dict = json.loads(my_str)

print(my_dict)

print(type(my_dict))

Let’s run the code to see the Python dictionary that the json.loads() method returns and confirm its type using the built-in type() method.

{'name': 'Will', 'age': 45, 'city': 'Los Angeles'}
<class 'dict'>

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!