TypeError: Object of type LegacyRow is not JSON serializable

What is TypeError: Object of type LegacyRow is not JSON serializable?

If you have ever encountered an error message like the following in your Python program:

TypeError: Object of type LegacyRow is not JSON serializable

This error message indicates that you are trying to convert a non-serializable Python object into a JSON format, which is not possible.

What are serializable objects?

In Python, objects that can be serialized are those that can be converted into a format that can be saved or transmitted over a network. Examples of serializable objects include strings, numbers, lists, and dictionaries. Serialization is used to save data in a way that it can be easily loaded back into memory later on.

What is JSON?

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

What is LegacyRow?

LegacyRow is a class in the google.cloud.bigquery.table.Row module. It is used to represent a row of data in a BigQuery table. LegacyRow is an older version of the Row class that was used in previous versions of the Google Cloud BigQuery client library for Python.

How to fix TypeError: Object of type LegacyRow is not JSON serializable?

If you encounter this error message in your Python program, there are several ways to fix it:

  • Convert LegacyRow to Row: If you are using an older version of the Google Cloud BigQuery client library for Python and you are using LegacyRow, you can try converting it to the newer version of the Row class. This may solve the problem.
  • Serialize using a custom encoder: You can write a custom JSON encoder that can handle non-serializable objects like LegacyRow. This encoder should subclass the json.JSONEncoder and override the default() method to handle the serialization of LegacyRow objects.
  • Serialize using a dictionary: You can convert the LegacyRow object into a dictionary and then serialize the dictionary instead. This can be done using the to_dict() method of the LegacyRow class.

Here is an example code snippet that shows how to serialize a LegacyRow object using a custom encoder:


import json
from google.cloud.bigquery.table import LegacyRow

class LegacyRowEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, LegacyRow):
            return obj.to_dict()
        return json.JSONEncoder.default(self, obj)

row = LegacyRow(('John', 42))
json_string = json.dumps(row, cls=LegacyRowEncoder)
print(json_string)

In this example, we define a custom JSON encoder called LegacyRowEncoder that subclasses json.JSONEncoder. The default() method of this encoder checks whether the object passed to it is an instance of LegacyRow. If it is, it converts the object into a dictionary using the to_dict() method of the LegacyRow class, which is then serialized using the json.dumps() function.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe