Menu Close

How to serialize a Decimal object into JSON with Python?

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.

Posted in Python, Python Answers