stripe: How to convert stripe model object into JSON to get complete hierarchical data?

Question:

How can I convert the stripe model object into JSON to receive complete hierarchical data at client end?

stripeCustomer = stripe.Customer.retrieve(<stripe customer id>)
sendResponseToClient(stripeCustomer)

I am receiving only 1st level of data as json at client end, second level data from Stripe JSON object is not formatted.

JSON Data Example: (2nd level of data is not received at client end,)

 Customer customer id=cus_DTWEPsfrHx3ikZ at 0x00000a> JSON: {
      "id": "cus_DTWEPsfrHx3ikZ",
      "object": "customer",
      "account_balance": 0,
      "created": 1535093686,
      "currency": "usd",
      "default_source": null,
      "delinquent": false,
      "description": null,
      "discount": null,
      "email": "[email protected]",
      "invoice_prefix": "E91FF30",
      "livemode": false,
      "metadata": {
      },
      "shipping": null,
      "sources": {
        "object": "list",
        "data": [

        ],
        "has_more": false,
        "total_count": 0,
        "url": "/v1/customers/cus_DTWEPsfrHx3ikZ/sources"
      },
      "subscriptions": {
        "object": "list",
        "data": [
          {
            "id": "sub_DTWEALN3urFael",
            "object": "subscription",
            "application_fee_percent": null,
            "billing": "charge_automatically",
            "billing_cycle_anchor": 1535093688,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "created": 1535093688,
            "current_period_end": 1537772088,
            "current_period_start": 1535093688,
            "customer": "cus_DTWEPsfrHx3ikZ",
            "days_until_due": null,
            "discount": null,
            "ended_at": null,
            "items": {
              "object": "list",
              "data": [
                {
                  "id": "si_DTWEuZaU4pw9Cv",
                  "object": "subscription_item",
                  "created": 1535093688,
                  "metadata": {
                  },
                  "plan": {
                    "id": "plan_free",
                    "object": "plan",
                    "active": true,
                    "aggregate_usage": null,
                    "amount": 0,
                    "billing_scheme": "per_unit",
                    "created": 1535008667,
                    "currency": "usd",
                    "interval": "month",
                    "interval_count": 1,
                    "livemode": false,
                    "metadata": {
                    },
                    "nickname": "free",
                    "product": "prod_DT8B8auk3CRNdw",
                    "tiers": null,
                    "tiers_mode": null,
                    "transform_usage": null,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                  },
                  "quantity": 1,
                  "subscription": "sub_DTWEALN3urFael"
                }
              ],
              "has_more": false,
              "total_count": 1,
              "url": "/v1/subscription_items?subscription=sub_DTWEALN3urFael"
            },
            "livemode": false,
            "metadata": {
            },
            "plan": {
              "id": "plan_free",
              "object": "plan",
              "active": true,
              "aggregate_usage": null,
              "amount": 0,
              "billing_scheme": "per_unit",
              "created": 1535008667,
              "currency": "usd",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {
              },
              "nickname": "free",
              "product": "prod_DT8B8auk3CRNdw",
              "tiers": null,
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "quantity": 1,
            "start": 1535093688,
            "status": "active",
            "tax_percent": null,
            "trial_end": null,
            "trial_start": null
          }
        ],
        "has_more": false,
        "total_count": 1,
        "url": "/v1/customers/cus_DTWEPsfrHx3ikZ/subscriptions"
      },
      "tax_info": null,
      "tax_info_verification": null
    }
Asked By: Rakesh Singh

||

Answers:

you can to_dict() api which will convert the stripe model object into dictionary format and then can be eventually converted into JSON string.

Answered By: Rakesh Singh

You can convert the returned Stripe object model to JSON using the following technique to ignore non-serializable fields:

stripeObject = stripe.SomeAPICall(...)
jsonEncoded = json.dumps(stripeObject, default=lambda o: '<not serializable>')
pythonDict = json.loads(jsonEncoded)
Answered By: Chris McCormick
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.