Demystifying TypeError: the json object must be str, bytes or bytearray, not dict with Causes and Solutions

In the world of programming, a popular programming language is Python, which supports multiple data types, such as strings, bytes, bytearrays, and dictionaries. JSON (JavaScript Object Notation) is a widely used format for exchanging data between applications. However, sometimes, you may encounter an error when working with JSON data in Python. The error message may look something like TypeError: the JSON object must be str, bytes, or bytearray, not dict

This error means you are trying to pass a dictionary object to a function that expects a JSON object as a string, bytes, or bytearray. This article will give you a walkthrough of the error and help you find ways to resolve it.

What is TypeError: the json object must be str, bytes or bytearray, not dict?

This error occurs when you try to use the json module in Python to load or dump a JSON object that is not a string, bytes, or bytearray. The json module provides two main functions for working with JSON data: json.load() and json.dump(). The json.load() function takes a file object or a string-like object as an argument and returns a Python object (such as a dictionary or a list) that corresponds to the JSON data. The json.dump() function takes a Python object and a file object or a string-like object as arguments and writes the JSON representation of the object to the file or the string-like object.

The error message tells you that the json object (the argument passed to the json.load() or json.dump() function) must be one of the following types: str, bytes, or bytearray. A str object is a sequence of Unicode characters that can represent text. A bytes object is a sequence of bytes that can represent binary data. A bytearray object represents binary data which is a mutable sequence of bytes. A dict object is a collection of key-value pairs representing a mapping or an associative array.

The error occurs because the json module expects a string-like object as an argument, not a dictionary. A dictionary is already a Python object that does not need to be converted to or from JSON. Suppose you pass a dictionary to the json.load() function will try to read it as a string and fail if you pass a dictionary to the json.dump() function, it will try to write it as a string and fail.

Why does the TypeError: the json object must be str, bytes or bytearray, not dict occur?

The TypeError: the json object must be str, bytes or bytearray, not dict occurs when you try to pass a dictionary object to a function that expects a JSON object in the form of a string, bytes, or bytearray. For example, suppose you have a file called data.json that contains some JSON data. You want to read this file and print its contents using the json module. You may write something like this:

import json

with open("data.json") as f:
  data = json.load(f)

print(data)

However, this code will raise an error:

TypeError: the JSON object must be str, bytes or bytearray, not dict 

This is because the json.load() function expects a file-like object that contains a JSON-formatted string, bytes, or bytearray. However, the data.json file is already a JSON object, not a JSON-formatted string, bytes, or bytearray. Therefore, the json.load() function cannot parse it and raises an error.

How to fix the TypeError: the json object must be str, bytes or bytearray, not dict?

There are two possible ways to fix the TypeError: the json object must be str, bytes or bytearray, not dict:

Use the json.loads() function instead of the json.load() function

The json.loads() function can take a JSON-formatted string, bytes, or bytearray and return a Python object. For example, you can modify the previous code as follows:

import json

with open("data.json") as f:
  data = json.loads(f.read())

print(data)

This code will read the data.json file as a string and pass it to the json.loads() function, which will return a Python dictionary.

Use the ast.literal_eval() function instead of the json module

The ast.literal_eval() function can take a string that contains a valid Python expression and return its value. Since a JSON object is also a valid Python expression, you can use this function to parse a JSON object. For example, you can modify the previous code as follows:

import ast

with open("data.json") as f:
  data = ast.literal_eval(f.read())

print(data)

This code will read the data.json file as a string and pass it to the ast.literal_eval() function, returning a Python dictionary.

Use the json.dumps() function instead of the json.load() function

The json.dumps() converts the native Python object or dictionary into a JSON string. Thus it will resolve the error the error by converting the dictionary into JSON strings.

Syntax:

import json

d1 = {"('Hello',)": 10, "('Hi',)": 9}
s1 = json.dumps(d1, indent=2)
print(s1)

The above code will convert the dictionary into the JSON formatted string.

FAQs

What is a JSON object in Python?

A JSON object is a data structure that follows the JSON syntax rules. It can be either an array (a list of values) or an object (a collection of key-value pairs). A JSON object can contain strings, numbers, booleans, nulls, arrays, and objects as its values.
In Python, one of the advantages is that it has a built-in json module that helps you work with JSON data. The json module provides two main functions: json.dumps() and json.loads(). The json.dumps() function takes a Python object and returns a JSON-formatted string. The json.loads() function takes a JSON-formatted string and returns a Python object.

Is there any difference between json.dump() and json.dumps() in Python?

One of the frequently asked questions about working with JSON data in Python is, if the json.dump() and json.dumps() functions are same or not. So the answer is No. The json.dump() function takes a Python object and a file-like object as arguments and writes the JSON-formatted data to the file. The json.dumps() function takes a Python object and returns a JSON-formatted string.

Conclusion

In this article, we have learned about the TypeError: the json object must be str, bytes or bytearray, not dict in Python. We have seen why this error occurs and how to fix it using either the json.loads() function or the ast.literal_eval() function.

Reference

  1. ast.literal_eval()
  2. Bytearray

To be up to date with our latest error handling posts follow pythonclear.com/errors.

Leave a Comment