You will get a TypeError
while converting dictionary to json using the json.loads() method.
You can solve the JSON object must be str, bytes or bytearray, not dict
error using the json.dumps() method instead of using the json.loads() method.
Basic Example
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict)
print(result)
print(type(result))
Output
{"Name": "Ram", "Job": "Expert", "Salary": "90000"}
<class 'str'>
This tutorial teaches you the cause and solution for the JSON object must be str, bytes or bytearray, not dict Error.
Also, you’ll learn the differences between the loads()
and the dumps()
methods.
Table of Contents
Cause
You’ll get the JSON object must be str, bytes or bytearray, not dict
error while using the json.loads() method.
Because the json.loads()
method parses a JSON string into a native Python object; hence, you cannot use the native Python object(A dictionary) in the loads()
method.
That is why the error is thrown while passing a dictionary to the loads()
method, as shown below.
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.loads(yourdict)
Output
TypeError: the JSON object must be str, bytes or bytearray, not dict
Solution
You can convert the Dictionary into a JSON string using the json.dumps() method.
The dumps()
method converts the native Python object into a JSON string.
Hence, it is used to convert a dictionary to a JSON string.
Return Value
- A JSON String equivalent to the passed dictionary
Code
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict)
print(result)
print(type(result))
Output
The type(result) is a string. The dictionary is converted into a JSON formatted string.
{"Name": "Ram", "Job": "Expert", "Salary": "90000"}
<class 'str'>
JSON Loads VS JSON Dumps
In this section, you’ll learn the different between JSON loads()
and the JSON dumps()
methods.
JSON Loads()
The loads()
method converts the JSON-formatted string into a native Python object.
Use the loads()
method to convert JSON formatted string into a dictionary.
Code
In the following code, the JSON string is converted into a dictionary.
The prefix r
in the string is used to make the String a raw string instead of a dictionary.
import json
yourdict = r'{"Name": "Ram", "Job": "Expert", "Salary": 90000}'
result = json.loads(yourdict)
print(result)
print(type(result))
Output
The string is converted into a Python dictionary.
{'Name': 'Ram', 'Job': 'Expert', 'Salary': 90000}
<class 'dict'>
JSON Dumps()
The dumps()
method converts Python native object into a JSON string
Use the dumps()
method to dictionary into a JSON formatted string.
Code
In the following code, the dictionary is converted into a JSON string.
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict)
print(result)
print(type(result))
Output
The Python dictionary is converted into a JSON-formatted string.
{"Name": "Ram", "Job": "Expert", "Salary": "90000"}
<class 'str'>