Attributeerror: dict object has no attribute dumps ( Solved )

Attributeerror dict object has no attribute dumps solved

Attributeerror: dict object has no attribute dumps error occurs while converting dict object to JSON object if dumps() function is invoked incorrectly. Since there is no dumps() function defined in the dict type class, we can not invoke dumps() function from the dict type object. In this article we will first replicate the error then we will see how can we resolve it.

Attributeerror: dict object has no attribute dumps ( Replication ) –

This error is generated when we invoke dumps() function from a dict-type object. Hence simply we will create a dict-type object and then invoke dumps() function from it. The Interpreter will throw this error. Here is a complete example of this.

AttributeError 'dict' object has no attribute 'dumps'
AttributeError ‘dict’ object has no attribute ‘dumps’

Attributeerror: dict object has no attribute dumps ( Solution ) –

In this section, we will see how can we fix this error.  See basically we need to take care below things while creating json objects out of the Python object dictionary.

  1. Import JSON Module.
  2. invoke json.dumps() function and pass dict as a parameter.

It will create JSON string for you. Here is the complete coding example for you.

import json
dict_sample ={1:'A', 2:'B'}
json_string = json.dumps(dict_sample)

In the above example, you can see that we have created a Python dictionary named dict_sample and then passed this dictionary as an argument into the json.dumps() function. Now let’s run this example and see the output.

End Notes :

Usually, We use  JSON format in transferring data from one endpoint to another ( API or services ).  Now suppose if we have created any utility in Python and want to transit from one source to another using API then we need to convert Python dict to JSON and sometimes we end up with the above error.

If you want to understand the solution principle for Attributeerror go for the below article:

What is AttributeError in Python ? Complete Overview



Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages, where he and his team share knowledge and help others learn more about data science.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner