BlackElefant
BlackElefant

Reputation: 37

Accessing nested data in a JSON API response

I have the following JSON file from a API request:

 'data': {
 '0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404': {
     'address': {
         'balance': '39674870110714259',
         'balance_usd': 90.9045327455033,
         'call_count': 10,
         'contract_code_hex': None,
         'contract_created': None,
         'contract_destroyed': None,
         'fees_approximate': '31500129889285741',
         'fees_usd': 72.17424235567398,
         'first_seen_receiving': '2021-03-14 '
         '00:58:46',
         'first_seen_spending': '2021-03-14 '
         '01:03:47',
         'last_seen_receiving': '2021-03-14 '
         '02:12:47',
         'last_seen_spending': '2021-03-14 '
         '02:30:59',
         'nonce': None,
         'received_approximate': '166671175000000000000',
         'received_usd': 320779.4692,
         'receiving_call_count': 4,
         'spending_call_count': 6,
         'spent_approximate': '166600000000000000000',
         'spent_usd': 320642.4844,
         'transaction_count': 10,
         'type': 'account'
     },
     'calls': [{
             'block_id': 12034014,
             'index': '0',
             'recipient': '0x57f41689847f8b0676a0ce8a7c5eb5fb78e79596',
             'sender': '0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404',
             'time': '2021-03-14 '
             '02:30:59',
             'transaction_hash': '0x777e01251d9c42df4269abcf3c72fd668653f68bf0d4716ef992e57c1bca641c',
             'transferred': True,
             'value': 2.29e+19,
             'value_usd': 44073.9069
         },
         {
             'block_id': 12034002,
             'index': '0',
             'recipient': '0xf582a0db4e787492921d0faee3e19f0e9edee261',
             'sender': '0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404',
             'time': '2021-03-14 '
             '02:29:30',
             'transaction_hash': '0xc70bdc0efd23bb303d27866806fde689b00fa6e1f8c61850d11e3684bad7d16c',
             'transferred': True,
             'value': 2.5e+19,
             'value_usd': 48115.6189
         }
     ]
 }

}

What I would like to do is to access the "recipient" section of the calls section for each call. I have tried it as following:

data=json_response['data']
calls=data['0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404']['calls']

for i in calls:
    recipient=calls[2]['recipient']
    print(recipient)

However this seems to always give the same recipient in the output

What am I doing wrong?

Upvotes: 0

Views: 41

Answers (1)

Camden Weaver
Camden Weaver

Reputation: 324

In your code you provided, you are not actually using the iterator i to access the calls, instead you are using calls[2] which will always get the same call each time.

Instead, use:

for i in calls:
    recipient=i['recipient']
    print(recipient)

Upvotes: 1

Related Questions