0

I have a piece of code like below

url = "https://blockchain.info/balance?active="+pub
r = requests.get(url)
print(r.json())

above code prints below output

{u'0jhke23k23k4j4k3434kk54l54': {u'final_balance': 0, u'n_tx': 0, u'total_received': 0}}

all I want is the final_balance and total_received

I tried below code

print("{} {:20} {:20}".format(pub, r.json()['final_balance'], r.json()['total_received']']))

but it gives me this error

KeyError: 'final_balance'

so how to get only final_balance from this output --> {u'0jhke23k23k4j4k3434kk54l54': {u'final_balance': 0, u'n_tx': 0, u'total_received': 0}}

??

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
AMendis
  • 1,346
  • 4
  • 18
  • 34
  • Welcome back to Stack Overflow. Please keep in mind that it if you received and parsed the JSON successfully, then *how* you got it does not matter to the *problem*, and therefore the *question*. Please read [mre]. In fact, it is not actually a question about JSON, but about *dictionaries*. (Also, please strongly consider upgrading your Python. It's free, and 2.7 has not been supported for more than two years. It is equivalent to using Windows 7 as your operating system, in terms of outdatedness.) – Karl Knechtel Jun 03 '22 at 05:11
  • "but it gives me this error" Well, yes; that isn't a key in the dictionary. There is one key in that dictionary: `u'0jhke23k23k4j4k3434kk54l54'`. Its value is *another* dictionary, which has the key you want. So you have to do the "look up in dictionary by key" logic twice. – Karl Knechtel Jun 03 '22 at 05:14

0 Answers0