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

Updated:   |  Python (Scripting) articles

Let's say something like this is being returned.

AttributeError: 'dict' object has no attribute 'json'

 

In this example, the issue is with a dict (dictionary) object. For example, if you were to do something like this.

#!/usr/bin/python3
dict = {'foo':'bar'}
print(f"dict = {dict}")
print(f"dict['foo'] = {dict['foo']}")
print(f"dict = {type(dict)}")

 

The following should be printed.

dict = {'foo': 'bar'}
dict['foo'] = bar
dict type = <class 'dict'>

 

But if you were to .json()

#!/usr/bin/python3
dict = {'foo':'bar'}
print(f"dict = {dict}")
print(f"dict.json()['foo'] = {dict.json()['foo']}")
print(f"dict = {type(dict)}")

 

The error would be returned.

AttributeError: 'dict' object has no attribute 'json'

 

In this example, this is occurring because my_dict is already a dictionary thus the exception is raised when attempting to parse the dictionary with .json. The solution is simply. Just don't use .json().

#!/usr/bin/python3
dict = {'foo':'bar'}
print(f"dict = {dict}")
print(f"dict['foo'] = {dict['foo']}")
print(f"dict = {type(dict)}")

 

 




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 cfd0fa in the box below so that we can be sure you are a human.