1

I'm calling up a lot of data through an API, and it's nested within many layers. I want to be able to reference pieces easily, with bracket notation ideally.

Right now, I'm pulling the data and it comes out like this:

   {
    'status': 'success', 
    'data': 
          {
           'available_balance': '0.1515', 
           'pending_withdrawals': '0.0000', 
           'withdrawable_balance': '0.1515', 
           'couponable_balance': '0.0000'
          }
    }

I can do balance["data"] which then outputs:

   {
    'available_balance': '0.1515', 
    'pending_withdrawals': '0.0000', 
    'withdrawable_balance': '0.1515', 
    'couponable_balance': '0.0000'
   }

But what I want is to do something like balance["data"["available_balance"]] and get:

0.1515

1
  • Have you tried balance["data"]["available_balance"] ? Commented Aug 18, 2019 at 4:17

1 Answer 1

3

Try below code:

balance['data']['available_balance']

JSON is loaded as nested dictionaries in python.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked, but if I were to have two different labels both named 'available_balance', how would I tell the program which to pick?
Well, there wouldn't be two 'available_balance' in the same scope, that doesn' t make much sense. Generally, we'd iterate over indexes in 'data' so we'd do something like: balance['data'][0]['available_balance'] , balance['data'][1]['available_balance'], balance['data'][2]['available_balance'], etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.