Python/Pandas使用Pandas.io.json.json_规范化以深入json

2024-10-01 11:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

以下json(也称为“Cus_数据”)是我正在使用pandas.io.json.json_normalize包进行解密的内容

我可以通过

cus_data = json_normalize(cus_data, 'data')

cus_data = json_normalize(cus_data['data'], max_level=1)

……不确定哪种做法是最佳做法

我似乎不知道如何输入“来源”、“订阅”、“计划”或任何子类别。我确信这是基本的,但我似乎无法理解在熊猫下使用这个软件包进行深入研究的概念。任何关于如何像这样从外部查看json的输入或提示都将非常有用

    {
  "data": [
    {
      "account_balance": 0,
      "address": null,
      "balance": 0,
      "created": 1589322628,
      "currency": "usd",
      "default_source": "card_1Gi6cGHdG7kiS0bLynjuRExn",
      "delinquent": false,
      "description": null,
      "discount": null,
      "email": "test1111@gmail.com",
      "id": "cus_HGduaDYZxYsNep",
      "invoice_prefix": "2469C675",
      "invoice_settings": {
        "custom_fields": null,
        "default_payment_method": null,
        "footer": null
      },
      "livemode": false,
      "metadata": {},
      "name": null,
      "next_invoice_sequence": 2,
      "object": "customer",
      "phone": null,
      "preferred_locales": [],
      "shipping": null,
      "sources": {
        "data": [
          {
            "address_city": null,
            "address_country": null,
            "address_line1": null,
            "address_line1_check": null,
            "address_line2": null,
            "address_state": null,
            "address_zip": null,
            "address_zip_check": null,
            "brand": "Visa",
            "country": "US",
            "customer": "cus_HGduaDYZxYsNep",
            "cvc_check": "unchecked",
            "dynamic_last4": null,
            "exp_month": 5,
            "exp_year": 2021,
            "fingerprint": "fEOTy80zKG0YbTwA",
            "funding": "credit",
            "id": "card_1Gi6cGHdG7kiS0bLynjuRExn",
            "last4": "5126",
            "metadata": {},
            "name": "test1111@gmail.com",
            "object": "card",
            "tokenization_method": null
          }
        ],
        "has_more": false,
        "object": "list",
        "total_count": 1,
        "url": "/v1/customers/cus_HGduaDYZxYsNep/sources"
      },
      "subscriptions": {
        "data": [
          {
            "application_fee_percent": null,
            "billing": "charge_automatically",
            "billing_cycle_anchor": 1589322628,
            "billing_thresholds": null,
            "cancel_at": null,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "collection_method": "charge_automatically",
            "created": 1589322628,
            "current_period_end": 1592001028,
            "current_period_start": 1589322628,
            "customer": "cus_HGduaDYZxYsNep",
            "days_until_due": null,
            "default_payment_method": null,
            "default_source": null,
            "default_tax_rates": [],
            "discount": null,
            "ended_at": null,
            "id": "sub_HGducAJ2Ox9H35",
            "invoice_customer_balance_settings": {
              "consume_applied_balance_on_void": true
            },
            "items": {
              "data": [
                {
                  "billing_thresholds": null,
                  "created": 1589322628,
                  "id": "si_HGduoRX8OyZSOj",
                  "metadata": {},
                  "object": "subscription_item",
                  "plan": {
                    "active": true,
                    "aggregate_usage": null,
                    "amount": 1999,
                    "amount_decimal": "1999",
                    "billing_scheme": "per_unit",
                    "created": 1589253059,
                    "currency": "usd",
                    "id": "plan_HGLCsqbTMnWJJU",
                    "interval": "month",
                    "interval_count": 1,
                    "livemode": false,
                    "metadata": {},
                    "nickname": "Monthly",
                    "object": "plan",
                    "product": "prod_HGLAvChw1qUMHd",
                    "tiers": null,
                    "tiers_mode": null,
                    "transform_usage": null,
                    "trial_period_days": 7,
                    "usage_type": "licensed"
                  },
                  "price": {
                    "active": true,
                    "billing_scheme": "per_unit",
                    "created": 1589253059,
                    "currency": "usd",
                    "id": "plan_HGLCsqbTMnWJJU",
                    "livemode": false,
                    "lookup_key": null,
                    "metadata": {},
                    "nickname": "Monthly",
                    "object": "price",
                    "product": "prod_HGLAvChw1qUMHd",
                    "recurring": {
                      "aggregate_usage": null,
                      "interval": "month",
                      "interval_count": 1,
                      "trial_period_days": 7,
                      "usage_type": "licensed"
                    },
                    "tiers": null,
                    "tiers_mode": null,
                    "transform_quantity": null,
                    "type": "recurring",
                    "unit_amount": 1999,
                    "unit_amount_decimal": "1999"
                  },
                  "quantity": 1,
                  "subscription": "sub_HGducAJ2Ox9H35",
                  "tax_rates": []
                }
              ],
              "has_more": false,
              "object": "list",
              "total_count": 1,
              "url": "/v1/subscription_items?subscription=sub_HGducAJ2Ox9H35"
            },
            "latest_invoice": "in_1Gi6cKHdG7kiS0bLVxWHYMMW",
            "livemode": false,
            "metadata": {},
            "next_pending_invoice_item_invoice": null,
            "object": "subscription",
            "pause_collection": null,
            "pending_invoice_item_interval": null,
            "pending_setup_intent": null,
            "pending_update": null,
            "plan": {
              "active": true,
              "aggregate_usage": null,
              "amount": 1999,
              "amount_decimal": "1999",
              "billing_scheme": "per_unit",
              "created": 1589253059,
              "currency": "usd",
              "id": "plan_HGLCsqbTMnWJJU",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "nickname": "Monthly",
              "object": "plan",
              "product": "prod_HGLAvChw1qUMHd",
              "tiers": null,
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": 7,
              "usage_type": "licensed"
            },
            "quantity": 1,
            "schedule": null,
            "start": 1589322628,
            "start_date": 1589322628,
            "status": "active",
            "tax_percent": null,
            "trial_end": null,
            "trial_start": null
          }
        ],
        "has_more": false,
        "object": "list",
        "total_count": 1,
        "url": "/v1/customers/cus_HGduaDYZxYsNep/subscriptions"
      },
      "tax_exempt": "none",
      "tax_ids": {},
      "tax_info": null,
      "tax_info_verification": null
    }
  ],
  "has_more": true,
  "object": "list",
  "url": "/v1/customers"
}

电流输出

  id    object  account_balance address  balance     created  \
0  cus_HGduaDYZxYsNep  customer                0    None        0  1589322628

  currency                 default_source  delinquent description discount  \
0      usd  card_1Gi6cGHdG7kiS0bLynjuRExn       False        None     None

                email invoice_prefix  livemode  name  next_invoice_sequence  \
0  test1111@gmail.com       2469C675     False  None                      2

  phone preferred_locales shipping tax_exempt tax_info tax_info_verification  \
0  None                []     None       none     None                  None

  invoice_settings.custom_fields invoice_settings.default_payment_method  \
0                           None                                    None

  invoice_settings.footer sources.object  \
0                    None           list

                                        sources.data  sources.has_more  \
0  [{'id': 'card_1Gi6cGHdG7kiS0bLynjuRExn', 'obje...             False

   sources.total_count                               sources.url  \
0                    1  /v1/customers/cus_HGduaDYZxYsNep/sources

  subscriptions.object                                 subscriptions.data  \
0                 list  [{'id': 'sub_HGducAJ2Ox9H35', 'object': 'subsc...

   subscriptions.has_more  subscriptions.total_count  \
0                   False                          1

                                subscriptions.url tax_ids.object tax_ids.data  \
0  /v1/customers/cus_HGduaDYZxYsNep/subscriptions           list           []

   tax_ids.has_more  tax_ids.total_count  \
0             False                    0

                                tax_ids.url
0  /v1/customers/cus_HGduaDYZxYsNep/tax_ids

Tags: noneidfalsedataobjectaddresssubscriptionscount
2条回答

以下内容使我进入嵌套数组

from pandas.io.json import json_normalize

stripe.api_key = "tes_api###############"

cus_data = stripe.Customer.list(limit=1)

cus_data = json_normalize(cus_data['data'],'subscriptions')

print(cus_data)

jmespath一个旋转;它可以帮助处理一些复杂的嵌套数据

要点:如果它是一个dict,您可以使用.符号访问它;如果它是一个数组/列表,则使用[]符号访问它。它可以是[*][]。你可以阅读docs了解更多关于这方面的信息

第一条路径是一个dict(data),它指向一个数组,其中包含一个dict。。。我们的终点是订阅阵列:

dict-> array ->dict - >array

实际代码:

import jmespath
expression = jmespath.compile('data[].subscriptions[]')
expression.search(content)

订阅数组是嵌套的。所以你需要确切地知道你想要什么,然后适应它。如果你确切地知道你的数据应该是什么样子,或者仍然有问题,只需发布你的预期输出,以及你在哪里遇到了问题,应该有人来帮助你

相关问题 更多 >