Select Page

How to Solve Python TypeError: Object of type method is not JSON serializable

by | Programming, Python, Tips

This error occurs when you pass the name of a method to the json.dumps() method instead of the method call. You can successfully pass a method call to json.dumps() if that method returns a JSON serializable object. You can solve this error by putting parentheses after the method name to call the method. For example,

json_str = json.dumps(method_name())

This tutorial will go through how to solve the error with code examples.


TypeError: Object of type method is not JSON serializable

Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type. The part “Object of type method” tells us the error is due to an illegal operation with a function.

Serialization in Python refers to converting a Python object into a transmittable format that we can recreate when needed using deserialization. JSON serialization returns a human-readable string form called a JSON string. The JSON encoder json.dump() and json.dumps() can only serialize certain object types like dictionaries, lists, or strings.

is not JSON serializable” informs us that the JSON serialization is an illegal operation for the method type.

Example

Let’s look at an example of a class that stores information about particles. We will add the class constructor method __init__() and a function called particle_info. Functions that belong to objects of a class are called methods, and the method particle_info returns a list containing the name, charge and mass in MeV of a particle.

import json

class Particle:

   def __init__(self, name, charge, mass):

       self.name = name

       self.charge = charge

       self.mass = mass

   def particle_info(self):

       particle_info = [self.name, self.charge, self.mass]

       return particle_info

Next, we will create an object of the Particle class to store information about the electron. We will then try to serialize the particle information by passing the particle_info method to the json.dumps() method call.

# Create an object of the Particle class

electron = Particle('electron', -1, 0.511)

# Attempt to serialize the particle info using the particle_info method

json_str = json.dumps(electron.particle_info)

Let’s run the code to see what happens:

TypeError: Object of type method is not JSON serializable

The error occurs because we passed the name of the method during the json.dumps() call instead of passing the method call. If we do not put parentheses () after the variable name electron.particle_info, the object is a method.

If we put parentheses after electron.particle_info, we call the method of the Particle object, which returns a list.

We can verify the difference between a method and a method call using the type() function as follows:

print(type(electron.particle_info))
print(type(electron.particle_info()))
<class 'method'>
<class 'list'>

The type of electron.particle_info is method and the type of electron.particle_info() is the type of the object that the method returns, which is list.

Note that method is distinct from a function in Python. Methods are functions associated with objects of a class. In this case, particle_info is a method of the Particle class. Functions are not associated with any object. For example, built-in functions like help().

You can read more about how to solve the error for objects of type: function by going to the article:

How to Solve Python TypeError: Object of type function is not JSON serializable

Solution

We can solve the error by passing the method call to json.dumps(). The method call returns the list containing the particle name, charge and mass, which is serializable.

Let’s look at the updated code:

electron = Particle('electron', -1, 0.511)

json_str = json.dumps(electron.particle_info())

print(json_str)

Let’s run the code to get the result:

["electron", -1, 0.511]

We successfully serialized the list containing the electron information to a JSON string.

Summary

Congratulations on reading to the end of this tutorial.

For further reading on errors involving JSON serialization, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!