Python Json Encode Decimal

Jul 26, 2019
import jsonimport decimalclass JsonEncoder(json.JSONEncoder):    def default(self, obj):        if isinstance(obj, decimal.Decimal):            return float(obj)        return json.JSONEncoder.default(self, obj)data = {    'float': 7.5,    'decimal': decimal.Decimal(7.5)}json.dumps(data, indent=2, cls=JsonEncoder)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.