Categories
Python Answers

How to serialize a Decimal object into JSON with Python?

Spread the love

Sometimes, we want to serialize a Decimal object into JSON with Python.

In this article, we’ll look at how to serialize a Decimal object into JSON with Python.

How to serialize a Decimal object into JSON with Python?

To serialize a Decimal object into JSON with Python, we can use the json.dumps method.

For instance, we write

json.dumps(Decimal('3.9'))

to call json.dumps with a Decimal object.

It returns the string with the Decimal number value.

Conclusion

To serialize a Decimal object into JSON with Python, we can use the json.dumps method.

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 *