Python (Scripting) - Resolve "AttributeError: 'dict' object has no attribute 'loads'"

Let's say something like this is being returned.

Traceback (most recent call last):
  File "example.py", line 504, in <module>
    json = json.loads(dictionary)
AttributeError: 'dict' object has no attribute 'loads'

 

This can occur if you use "json" as the name of the variable that stores the json.loads or json.dumps output. 

In other words, do not use json = json.dumps(dictionary) as this overrides the json object, causing all sorts of issues.

#!/usr/bin/python
import json
dictionary = { "name": "John Doe" }
json = json.dumps(dictionary)

 

Once I got stuck for a bit where I was using json.loads in two different blocks of my script. I was looking at the blocks near lines 101 and 102 and they looked fine. It was only after looking closely at the traceback output that I noticed the issue was at line 504.

0   #!/usr/bin/python
1   import json
101 dict1 = { "name": "John Doe" }
102 json_load = json.loads(dict1)
503 dict2 = { "name": "Jane Doe" }
504 json = json.loads(dict2)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 026b4 in the box below so that we can be sure you are a human.