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

Updated:   |  Python (Scripting) articles

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



Comments


Add a Comment


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