How to fix the “TypeError: Object of type Timestamp is not JSON serializable” in Python

TypeError: Object of type Timestamp is not JSON serializable

“TypeError: Object of type Timestamp is not JSON serializable” is a complex Python error. Fortunately, we have a few solutions for this error. This article will discuss the solutions in detail, so keep reading to fix this bug!

What is JSON?

JSON is a data format that follows a certain rule and can be read by most programming languages today. JSON is an open standard for data exchange on the web.

In Python, there are several ways to serialize objects into JSON format. The most common way is using the JSON module. The JSON module includes an API for converting Python objects to JavaScript Object Notation (JSON).

When does the message “TypeError: Object of type Timestamp is not JSON serializable” appear?

This error occurs when converting a Python timestamp object into JSON format using json.dumps(). The reason is that JSON only supports a subset of data types, and timestamp objects are not one of those data types. Examine the example below:

import json
import pandas

# Create a timestamp object
timestampObj = pandas.Timestamp('2022-12-11')

# Try to serialize the timestamp object
timestampJson = json.dumps({'Release date': timestampObj})
print(timestampJson)

Error:

TypeError: Object of type Timestamp is not JSON serializable

How to fix this error?

The solution to this error is converting the timestamp object to the string object before converting it to JSON format. Here are some approaches for converting the timestamp object to the string object.

Using the str() function

The str() function is a Python built-in function that returns a string representation of an object. We can use this function to print data to the console or store data in string format.

Since json.dumps() does not support timestamp objects, we will convert the object to the string object before passing it to this function. For example:

import json
import pandas

# Create a timestamp
timestampObj = pandas.Timestamp('2022-12-11')

# Convert the timestampObj to a string using str() and then put it in json.dumps()
timestampJson = json.dumps({'Release date': str(timestampObj)})
print(timestampJson)

Output:

{"Release date": "2022-12-11 00:00:00"}

Using the strftime() function

Python’s strftime() function allows you to format dates and times according to a specified string format. This function returns a string value, the data type we must pass into json.dumps().

Syntax:

strftime(time, format)

Parameters:

  • time: a time object or date object
  • format: the format that you want to apply to the time.

In this case, we’ll use the strftime() function to convert the timestamp object to the string before passing it to json.dumps(). Like this:

import json
import pandas
from datetime import datetime

# Create a timestamp
timestampObj = pandas.Timestamp('2022-12-11')

# Convert the timestampObj to a string using strftime() and then put it in json.dumps()
timestampJson = json.dumps(
    {
        'Release date': datetime.strftime(timestampObj, '%Y %m %d')
    }
)
print(timestampJson)

Output:

{"Release date": "2022 12 11"}

Summary

To summarize, the main cause of the TypeError: Object of type Timestamp is not JSON serializable is that you are attempting to use an object of type timestamp for json.dumps(). To avoid this error in the future, use a string instead of an object of type timestamp for json.dumps().

Have a beautiful day!

Maybe you are interested:

Leave a Reply

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