Fixing “TypeError: Object of type DataFrame is not JSON serializable” in Python

TypeError: Object of type DataFrame is not JSON serializable in Python

If you are getting trouble with the error “TypeError: Object of type DataFrame is not JSON serializable” in Python, keep reading our article. We will give you some methods to handle the problem.

Reason for “TypeError: Object of type DataFrame is not JSON serializable” in Python

In Python, we can only convert a few types into JSON formats, such as int, dict, list, str, and float,… Therefore, you will receive an error when trying to convert other types to JSON format.

For example:

import pandas as pd
import json
 
link = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/countries.csv" 
data = pd.read_csv(link).sample(n = 4)
jsonData = json.dumps(data)

Result:

Traceback (most recent call last)
in <module>
----> 6 jsonData = json.dumps(data)
TypeError: Object of type DataFrame is not JSON serializable

We will reset the index and take a look at the data table:

Code:

import pandas as pd
import json
 
link = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/countries.csv" 
data = pd.read_csv(link).sample(n = 4)
data.reset_index(drop = True, inplace = True)

print(data)

Result:

          Country   Region
0           Libya   AFRICA
1  Czech Republic   EUROPE
2         Vanuatu  OCEANIA
3           Syria     ASIA

Solutions to fix this problem

Using to_dict() function

The to_dict() function converts the DataFame type to a dictionary – the valid type to feed JSON input.

Syntax:

to_dict(orient, into)

Parameters:

  • orient: Type of values of the dictionary. Default ‘dict’
  • into: Class or instance to map

Input:

import pandas as pd
import json
 
link = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/countries.csv" 
data = pd.read_csv(link).sample(n = 4) 
validData = data.to_dict()
jsonData = json.dumps(validData)
 
print(jsonData)

Result:

{"Country": {"0": "Namibia", "1": "Zambia", "2": "Trinidad and Tobago", "3": "Lebanon"}, "Region": {"0": "AFRICA", "1": "AFRICA", "2": "NORTH AMERICA", "3": "ASIA"}}

Using to_json() function

This function converts the DataFame type to a JSON string – the valid type to feed JSON input.

Syntax:

to_json()

Code:

import pandas as pd
import json
 
link = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/countries.csv" 
data = pd.read_csv(link).sample(n = 4) 
validData = data.to_json()
jsonData = json.dumps(validData)
 
print(jsonData)

Result:

{"Country": {"0": "Namibia", "1": "Zambia", "2": "Trinidad and Tobago", "3": "Lebanon"}, "Region": {"0": "AFRICA", "1": "AFRICA", "2": "NORTH AMERICA", "3": "ASIA"}}

Custom JSONEncoder class

See our previous article to understand the JSONEncoder class.

In this situation, the class looks like that:

class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, pd.DataFrame):
            return obj.to_json()
        return json.JSONEncoder.default(self, obj)

In this method, you also using to_dict() function instead of to_json() function.

Full code:

import pandas as pd
import json
 
link = "https://raw.githubusercontent.com/VN-huster/LearnShareIT/main/countries.csv" 
data = pd.read_csv(link).sample(n = 4)
 
class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, pd.DataFrame):
            return obj.to_dict()
        return json.JSONEncoder.default(self, obj)
      
jsonData = json.dumps(data, cls=JSONEncoder)
print(jsonData)

Result:

{"Country": {"0": "Namibia", "1": "Zambia", "2": "Trinidad and Tobago", "3": "Lebanon"}, "Region": {"0": "AFRICA", "1": "AFRICA", "2": "NORTH AMERICA", "3": "ASIA"}}

Summary

We have presented solutions to fix the error “TypeError: Object of type DataFrame is not JSON serializable” in Python with detailed implementations. We hope our article is helpful to you. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *