Reputation: 116
I have a JSON that looks like this (I don't care about the info, is not anything important):
{
"data": [
{
"application_fee_percent": null,
"billing": "charge_automatically",
"billing_cycle_anchor": 1611687866,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "charge_automatically",
"created": 1611687866,
"current_period_end": 1643223866,
"current_period_start": 1611687866,
"customer": "cus_Ipc4utw6iwpjzG",
"days_until_due": null,
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [],
"discount": {
"checkout_session": null,
"coupon": {
"amount_off": 25,
"created": 1611687864,
"currency": "usd",
"duration": "repeating",
"duration_in_months": 3,
"id": "eGE2X5WO",
"livemode": false,
"max_redemptions": 10,
"metadata": {
"test": "test"
},
"name": "25 off",
"object": "coupon",
"percent_off": null,
"redeem_by": 1766448000,
"times_redeemed": 1,
"valid": true
},
"customer": "cus_Ipc4utw6iwpjzG",
"end": 1619463866,
"id": "di_1IDwpy2eZvKYlo2CO9NXr9NJ",
"invoice": null,
"invoice_item": null,
"object": "discount",
"promotion_code": "promo_1IDwpw2eZvKYlo2Co0OUIl0m",
"start": 1611687866,
"subscription": "sub_Ipc4diRpUv2E5J"
},
"ended_at": null,
"id": "sub_Ipc4diRpUv2E5J",
"invoice_customer_balance_settings": {
"consume_applied_balance_on_void": true
},
"items": {
"data": [
{
"billing_thresholds": null,
"created": 1611687867,
"id": "si_Ipc45R9qX6aYwb",
"metadata": {},
"object": "subscription_item",
"plan": {
"active": true,
"aggregate_usage": null,
"amount": 5,
"amount_decimal": "5",
"billing_scheme": "per_unit",
"created": 1599143565,
"currency": "usd",
"id": "price_1HNJUX2eZvKYlo2C02FTibzq",
"interval": "year",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"object": "plan",
"product": "prod_HwQjfoxyonlfag",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"active": true,
"billing_scheme": "per_unit",
"created": 1599143565,
"currency": "usd",
"id": "price_1HNJUX2eZvKYlo2C02FTibzq",
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"object": "price",
"product": "prod_HwQjfoxyonlfag",
"recurring": {
"aggregate_usage": null,
"interval": "year",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 5,
"unit_amount_decimal": "5"
},
"quantity": 1,
"subscription": "sub_Ipc4diRpUv2E5J",
"tax_rates": []
}
],
"has_more": false,
"object": "list",
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_Ipc4diRpUv2E5J"
},
"latest_invoice": "in_1IDwpy2eZvKYlo2ChABu4OXQ",
"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": 5,
"billing_scheme": "per_unit",
"created": 1599143565,
"currency": "usd",
"id": "price_1HNJUX2eZvKYlo2C02FTibzq",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_HwQjfoxyonlfag",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start": 1611687866,
"start_date": 1611687866,
"status": "active",
"tax_percent": null,
"transfer_data": null,
"trial_end": null,
"trial_start": null
}
],
"has_more": false,
"object": "list",
"url": "/v1/subscriptions"
}
So, the thing is that I'm trying to access the "id" value, inside the "data" array on the JSON doing : jsondata.data.id
but it says the following: AttributeError: 'list' object has no attribute 'id'
I actually don't know where the error is, and I would appreciate some help. Thanks
Upvotes: 0
Views: 40
Reputation: 116
As the content of "data" is an array, we would have to access the first element of the array, so we just would have to change it to
jsondata.data[0].id
Upvotes: 1
Reputation: 825
jsondata.data
is a list. Iterating over it will give you the element you want.
for item in jsondata.data:
print(item.id)
Upvotes: 0