Categories
Python Answers

How to fix “datetime.datetime not JSON serializable” with Python?

Spread the love

Sometimes, we want to fix "datetime.datetime not JSON serializable" with Python.

In this article, we’ll look at how to fix "datetime.datetime not JSON serializable" with Python.

How to fix "datetime.datetime not JSON serializable" with Python?

To fix "datetime.datetime not JSON serializable" with Python, we can use the json.dumps method with the default argument set to str.

For instance, we write

s = json.dumps(my_dictionary, indent=4, sort_keys=True, default=str)

to serialize the my_dictionary dict to a JSON string by calling it with the default argument set to str.

This will convert anything that’s not serializable to a string, including datetime objects.

Conclusion

To fix "datetime.datetime not JSON serializable" with Python, we can use the json.dumps method with the default argument set to str.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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