Reputation: 349
Python 2.6.6
I make a GET request and am handed an array of JSON objects. Here's an example:
{
"data": [
{
"amount": 59,
"amount_refunded": 0,
"balance_transaction": " XXXXXXXXXXXXx",
"captured": true,
"card": {
"address_line1_check": "fail",
"address_line2": null,
"address_zip_check": "fail",
"brand": "MasterCard",
"customer": null,
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 8,
"exp_year": 2016,
"fingerprint": "XXXXXXXXXXXXXX",
"funding": "prepaid",
"id": "XXXXXXXXXXXXXXXXXXXXX",
"last4": "1111",
"object": "card",
"type": "MasterCard"
},
"created": 11111111111111,
"currency": "gbp",
"customer": null,
"dispute": null,
"failure_code": null,
"failure_message": null,
"id": "ch_56Lce",
"invoice": null,
"livemode": true,
"metadata": {},
"object": "charge",
"paid": true,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": [],
"shipping": null,
"statement_description": null
},
{
"amount": 9,
"amount_refunded": 0,
"balance_transaction": "XXXXXXXXXXXX",
"captured": true,
"card": {
"address_line1_check": "fail",
"address_line2": null,
"address_zip_check": "fail",
"brand": "MasterCard",
"country": "BZ",
"customer": null,
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 1,
"exp_year": 2016,
"fingerprint": "XXXXXXXXXXXXXXXXXXXX",
"funding": "prepaid",
"id": "XXXXXXXXXXXXXXXXX",
"last4": "1111",
"object": "card",
"type": "MasterCard"
},
"created": 11111111111111111111111111,
"currency": "gbp",
"customer": null,
"dispute": null,
"failure_code": null,
"failure_message": null,
"id": "xxxxxxxxxxxxxxxX",
"invoice": null,
"livemode": true,
"metadata": {},
"object": "charge",
"paid": true,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": [],
"shipping": null,
"statement_description": null
},
{
"amount": 9,
"amount_refunded": 0,
"balance_transaction": "XXXXXXXXXXXXXXXXXXXXXXXXX",
"captured": true,
"card": {
"address_line2": null,
"address_zip_check": "pass",
"brand": "Visa",
"country": "US",
"customer": null,
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 1,
"exp_year": 2218,
"fingerprint": "XXXXXXXXXXXXXXXXXXXXXXX",
"funding": "debit",
"id": "GGGGGGGGGGGGGGGGGGGG",
"object": "card",
"type": "Visa"
},
"created": 1111111111111111111,
"currency": "usd",
"customer": null,
"dispute": null,
"failure_code": null,
"failure_message": null,
"id": "SXXXXXXXXXXXXXXXXXXXXX",
"invoice": null,
"livemode": true,
"metadata": {},
"object": "charge",
"paid": true,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": [],
"shipping": null,
"statement_description": null
},
{
"amount": 9,
"amount_refunded": 0,
"balance_transaction": null,
"captured": false,
"card": {
"address_zip_check": "unchecked",
"brand": "Visa",
"country": "US",
"customer": null,
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 7,
"exp_year": 2089,
"fingerprint": "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
"funding": "debit",
"id": "XXXXXXXXXXXXXXX",
"last4": "11111",
"object": "card",
"type": "Visa"
},
"created": 111111111111111,
"currency": "usd",
"customer": null,
"dispute": null,
"failure_code": "card_declined",
"failure_message": "Your card was declined.",
"id": "XXXXXXXXXXXXXXXXXXXXXXXX",
"invoice": null,
"livemode": true,
"metadata": {},
"object": "charge",
"paid": false,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": [],
"shipping": null,
"statement_description": null
},
{
"amount": 9,
"amount_refunded": 0,
"balance_transaction": null,
"captured": false,
"card": {
"address_zip_check": "fail",
"brand": "Visa",
"country": "US",
"customer": null,
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 5,
"exp_year": 2222,
"fingerprint": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"funding": "debit",
"id": "XXXXXXXXXXXXXXXXX",
"last4": "1111",
"object": "card",
"type": "Visa"
},
"created": 11111111111111111,
"currency": "usd",
"customer": null,
"dispute": null,
"failure_code": "card_declined",
"failure_message": "Your card was declined.",
"id": "XXXXXXXXXXXXXXXXXXXX",
"invoice": null,
"livemode": true,
"metadata": {},
"object": "charge",
"paid": false,
"receipt_email": null,
"receipt_number": null,
"refunded": false,
"refunds": [],
"shipping": null,
"statement_description": null
}
],
"has_more": true,
"object": "list",
"url": "/v1/charges"
}
I want to iterate over each item in the array and display the item's object and card.object property values. I'm not having any luck with all my attempts. Here's one:
charges = s.Charge.all(limit=5)
for key, value in charges.iteritems():
#print value.data['object']
#print value.data.card['object']
# Above doesn't work, let's try this:
print value.data.object
print value.data.card.object
# or this
print key.data.object
print key.data.card.object
print("done")
This is embarrassing. Please help!
Upvotes: 0
Views: 74
Reputation: 2207
charges.iteritems()
will iterate over the keys and values of charges, which is presumably the outermost dictionary you listed in your example. If so, then you're looping over the following keys: "data", "has_more", "object" and "url". Note that only "data" has a list as its corresponding value, and it's not a dictionary, so iteritems()
isn't applicable.
You should be iterating over the list charges["data"]. Each of the items in this list are dictionaries, with "object" and "card" keys. Try something like this
charges = s.Charge.all(limit=5)
for value in charges["data"]:
print value["object"]
print value["card"]["object"]
Upvotes: 1
Reputation: 599926
I'm not sure why you're trying a combination of dot and bracket lookups. This is a nested dictionary: dot notation has nothing whatever to do here. The one thing you don't seem to have tried, though, is to use brackets notation throughout.
Assuming that this blob is the entirety of a single Charge object, you can do the following:
data = charge['data']
for item in data: # data is a list of dictionaries
card = item['card']
object = card['object']
... etc ...
Upvotes: 1