Object of type Decimal is not JSON serializable while passing in decimal field in serializer

I am trying to pass in some more data to the jwt token authentication, i have written a Serialize for this and passed in some other fields like full_name, image, pin etc now i am trying to pass in the wallet field which is a DecimalField in my Model, then i am getting this error. Object of type Decimal is not JSON serializable how do i fix this?

token['wallet'] = user.profile.wallet

Complete Serializer

class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)

        # Add custom claims
        token['username'] = user.username
        token['email'] = user.email
        token['full_name'] = user.profile.full_name
        token['bio'] = user.profile.bio
        token['country'] = user.profile.country
        token['image'] = user.profile.image.url
        token['address'] = user.profile.address
        token['phone'] = user.profile.phone
        token['website'] = user.profile.website
        token['pin'] = user.profile.pin
        token['verified'] = user.profile.verified
        token['wallet'] = user.profile.wallet

        return token

Do i need to convert the wallet field to a decimal field first?

Is wallet a Decimal field? If so, you want to serialize it as a string, not as a floating point value.

1 Like

Yes it is a decimal field and please how do i go about serialzing it to become a string?

How do you convert any Decimal value to a string?

I don’t know if this is the right way to do it, but i just wrapped it in the str() method and it worked.

token['wallet'] = str(user.profile.wallet)
1 Like