-1

Good afternoon!

I am a very new python programmer who is mainly self-taught and am 4 weeks into a college python course. I'm working on a personal python project that needs me to take a part of an API response and use it to make a variable. The data I get is

{'id': '3f69d086-530e-401e-8b30-xxxxxxxxxxxx', 
 'currency': 'BTC', 
 'balance': '9.0987318000000000', 
 'hold': '0.0000000000000000', 
 'available': '9.0987318', 
 'profile_id': '3945058c-8681-4c8b-a69e-xxxxxxxxxxxx', 
 'trading_enabled': True
}

and I need to assign the balance to a variable "coins".

If anyone has any suggestions as to go about this, I'd be greatly appreceated!

Thanks!

EDIT:

So far I've got this:

account_resposne = auth_client.get_account("3f69d086-530e-401e-8b30-xxxxxxxxxxxx")
data = account_resposne.json()
coins = data["balance"]
        
print(coins)

but it keeps giving me an error of

data = account_resposne.json()
AttributeError: 'dict' object has no attribute 'json'
  • What have you tried so far? – Klaus D. Oct 04 '21 at 20:27
  • 2
    Use the `json` module to turn the JSON response into a Python `dict`, and then `coins = data["balance"]` where `data` is the Python dictionary you create to hold the response data. – ddejohn Oct 04 '21 at 20:27

1 Answers1

0

What you received is known as a JSON Object

First, assign it to a dictionary, Python's version of objects.

data = response.json()

Then access and assign

coins= data["balance"]

Read about JSON and Python dictionaries. They are some of the easiest data to work with.

Libra
  • 2,544
  • 1
  • 8
  • 24
  • ok, I've tried it out, but I'm still getting errors. Thank you so much for working with me! Heres what I have now: account_resposne = auth_client.get_account("3f69d086-530e-401e-8b30-9404e6313f90") data = account_resposne.json() coins = data["balance"] print(coins) – Samuel Bemis Oct 04 '21 at 20:39
  • @SamuelBemis Are you getting an error? What's the problem? – Libra Oct 04 '21 at 20:43
  • I just added it to the main body queston – Samuel Bemis Oct 04 '21 at 20:45
  • @SamuelBemis it seems like your object is already a dictionary for some reason or another, so you do not need to call the .json() method on it, just use it directly instead of `data` – Libra Oct 04 '21 at 20:47