0

I am new to python, and am writing an API for my project.

I am able to get the object, however whenever i try to do json.dumps(sub_entry.__dict__), it gives the error Object of type Subscription is not JSON serializable.

I suspect it is because of this '_response_obj': {'subscription': <chargebee.models.subscription.Subscription object at 0x10307ceb8>

The _response_obj above was not in the orignal sub_entry and only showed after printing sub_entry.__dict__

Can anyone help me ?

sub_entry:

{
    "subscription": {
        "id": "16COUVSU397xz4Cg9",
        "plan_id": "P04",
        "plan_quantity": 1,
        "plan_unit_price": 6800,
        "billing_period": 1,
        "billing_period_unit": "month",
        "customer_id": "16COUVSU397xz4Cg9",
        "plan_amount": 6800,
        "plan_free_quantity": 0,
        "status": "active",
        "current_term_start": 1618325269,
        "current_term_end": 1620917269,
        "next_billing_at": 1620917269,
        "created_at": 1617937215,
        "started_at": 1617937215,
        "activated_at": 1617937215,
        "created_from_ip": "182.239.117.243",
        "updated_at": 1618325278,
        "has_scheduled_changes": false,
        "resource_version": 1618325278117,
        "deleted": false,
        "object": "subscription",
        "coupon": "TEST-FREE",
        "currency_code": "HKD",
        "coupons": [
            {
                "coupon_id": "TEST-FREE",
                "applied_count": 2,
                "object": "coupon"
            }
        ],
        "due_invoices_count": 0,
        "mrr": 0,
        "exchange_rate": 1.0,
        "base_currency_code": "HKD"
    },
    "customer": {
        "id": "16COUVSU397xz4Cg9",
        "email": "liam.leung@binery.co",
        "auto_collection": "on",
        "net_term_days": 0,
        "allow_direct_debit": false,
        "created_at": 1617937215,
        "created_from_ip": "182.239.117.243",
        "taxability": "taxable",
        "updated_at": 1618322530,
        "pii_cleared": "active",
        "resource_version": 1618322530322,
        "deleted": false,
        "object": "customer",
        "billing_address": {
            "first_name": "Liam",
            "last_name": "Leung",
            "line1": "Hk",
            "city": "Hong Kong",
            "country": "HK",
            "validation_status": "not_validated",
            "object": "billing_address"
        },
        "card_status": "valid",
        "promotional_credits": 0,
        "refundable_credits": 0,
        "excess_payments": 0,
        "unbilled_charges": 0,
        "preferred_currency_code": "HKD",
        "primary_payment_source_id": "pm_Azq8bBSU39g1k4CdR",
        "payment_method": {
            "object": "payment_method",
            "type": "card",
            "reference_id": "cus_JGhzp8fcSX2p6D/pm_1IeAZsEEy6BVvQ9BSRZPFda2",
            "gateway": "stripe",
            "gateway_account_id": "gw_16CKhcSQyHkLn5rW",
            "status": "valid"
        }
    },
    "card": {
        "status": "valid",
        "gateway": "stripe",
        "gateway_account_id": "gw_16CKhcSQyHkLn5rW",
        "iin": "******",
        "last4": "7616",
        "card_type": "mastercard",
        "funding_type": "debit",
        "expiry_month": 7,
        "expiry_year": 2025,
        "issuing_country": "HK",
        "created_at": 1617937216,
        "updated_at": 1617937216,
        "ip_address": "182.239.117.243",
        "powered_by": "not_applicable",
        "resource_version": 1617937216831,
        "object": "card",
        "masked_number": "************7616",
        "customer_id": "16COUVSU397xz4Cg9",
        "payment_source_id": "pm_Azq8bBSU39g1k4CdR"
    }
}

sub_entry._ _ dict _ _:

{'_response': {'subscription': {'id': '21433j2fadsf', 'plan_id': 'P04', 'plan_quantity': 1, 'plan_unit_price': 6800, 'billing_period': 1, 'billing_period_unit': 'month', 'customer_id': '234fasdfe3fdsa', 'plan_amount': 6800, 'plan_free_quantity': 0, 'status': 'active', 'current_term_start': 1618325269, 'current_term_end': 1620917269, 'next_billing_at': 1620917269, 'created_at': 1617937215, 'started_at': 1617937215, 'activated_at': 1617937215, 'created_from_ip': '182.239.117.243', 'updated_at': 1618325278, 'has_scheduled_changes': False, 'resource_version': 1618325278117, 'deleted': False, 'object': 'subscription', 'coupon': 'TEST-FREE', 'currency_code': 'HKD', 'coupons': [{'coupon_id': 'TEST-FREE', 'applied_count': 2, 'object': 'coupon'}], 'due_invoices_count': 0, 'mrr': 0, 'exchange_rate': 1.0, 'base_currency_code': 'HKD'}, 'customer': {'id': '16COUVSU397xz4Cg9', 'email': 'email@email.co', 'auto_collection': 'on', 'net_term_days': 0, 'allow_direct_debit': False, 'created_at': 1617937215, 'created_from_ip': '182.239.117.243', 'taxability': 'taxable', 'updated_at': 1618322530, 'pii_cleared': 'active', 'resource_version': 1618322530322, 'deleted': False, 'object': 'customer', 'billing_address': {'first_name': 'Liam', 'last_name': 'Leung', 'line1': 'Hk', 'city': 'Hong Kong', 'country': 'HK', 'validation_status': 'not_validated', 'object': 'billing_address'}, 'card_status': 'valid', 'promotional_credits': 0, 'refundable_credits': 0, 'excess_payments': 0, 'unbilled_charges': 0, 'preferred_currency_code': 'HKD', 'primary_payment_source_id': 'pm_q34f32dd3', 'payment_method': {'object': 'payment_method', 'type': 'card', 'reference_id': 'cus_JAhzpfa3fD8fcSX2p6D/pm_1IeAZs5yfaGg5vQ9BSRZFda2', 'gateway': 'stripe', 'gateway_account_id': 'gw_16CKhcSQyHkLn5rW', 'status': 'valid'}}, 'card': {'status': 'valid', 'gateway': 'stripe', 'gateway_account_id': 'gw_16CKhcSQyHkLn5rW', 'iin': '******', 'last4': '7616', 'card_type': 'mastercard', 'funding_type': 'debit', 'expiry_month': 7, 'expiry_year': 2025, 'issuing_country': 'HK', 'created_at': 1617937216, 'updated_at': 1617937216, 'ip_address': '182.239.117.243', 'powered_by': 'not_applicable', 'resource_version': 1617937216831, 'object': 'card', 'masked_number': '************8777', 'customer_id': '16IUUV6ER77xz4Cg9', 'payment_source_id': 'pm_Azq8bBSU39g1k4CdR'}}, '_response_obj': {'subscription': <chargebee.models.subscription.Subscription object at 0x10307ceb8>}}
liam
  • 189
  • 2
  • 2
  • 6
  • 1
    you need to convert that `chargebee.models.subscription.Subscription` object to json/dict/str format first – sahasrara62 Apr 14 '21 at 03:47
  • @sahasrara62 that `chargebee.models.subscription.Subscription` object was not seen in `sub_entry`, and it is only showing after doing `sub_entry.__dict__`, I am not sure what I can do here – liam Apr 14 '21 at 03:54
  • perhaps this will help https://stackoverflow.com/questions/10252010/serializing-class-instance-to-json – Derek Eden Apr 14 '21 at 03:58
  • i suggest you to not to use `obj.__dict__` to get the data from table model (considering this is table data from orm) you can use `{c.name: getattr(self, c.name) for c in self.__table__.columns}` to get all the table data in json format – sahasrara62 Apr 14 '21 at 04:09
  • I have solved it by doing: `json.dumps(sub_entry, default=lambda x: getattr(x, '__dict__', str(x)))` Thanks both !! – liam Apr 14 '21 at 04:15

1 Answers1

1

You can use the .values attribute available in the response object returned by Chargebee.

In your case, try using sub_entry.values instead .

AlexPnt
  • 579
  • 5
  • 9