0

I am working with a cryptocurrency exchange API and I am stuck on trying to get it to pull just the symbol and last from the list from the API. I found many solutions if the list was in the code itself, but I am having trouble pulling from the API then just the two desired items, fields, whatever you would like to call it, but when checking for keys I get None. I have tried many methods found on this site and tutorials by others, but keep running into errors. Here is my code to talk to the API:

def pull():
    r = requests.get('API_ADDRESS')
    data = r.json()
    print(type(data)) # confirms type is list

output: class 'list'

Here is a small chunk from the API URL from the beginning/top of the page via browser:

[{"ask":"0.00000019438","bid":"0.00000019436","last":"0.00000019437","open":"0.00000017778","low":"0.00000017500","high":"0.00000020000","volume":"215392000","volumeQuote":"39.177612838","timestamp":"2019-03-12T22:54:35.168Z","symbol":"BCNBTC"},{"ask":"3886.77","bid":"3886.76","last":"3886.76","open":"3868.90","low":"3814.66","high":"3903.39","volume":"33598.51324","volumeQuote":"129840974.9750335","timestamp":"2019-03-12T22:55:44.772Z","symbol":"BTCUSD"},{"ask":"0.023432","bid":"0.023403","last":"0.023404","open":"0.021257","low":"0.021208","high":"0.023639","volume":"78033.270","volumeQuote":"1720.630098144","timestamp":"2019-03-12T22:55:38.179Z","symbol":"DASHBTC"},

Per the API documentation using curl is how to pull the data so I wonder if I am pulling it down the wrong way and that is part of the issue? The full list is all the currencies supported starting with a [ and ending with a ], each currency open and closes with {}.

I have been working with APIs like this that are dicts and have keys with no problem:

{  
   'items':[  
      {  
        'created_on':'2016-11-08',
        'etag':'DELETED',
        'classification':{  
        'type':'charge-description',
        'description':'A registered charge'

I wuld like to pull in order just the symbol, last. The only way I have been able to get the symbol or last to print is add [0] in front, but then only prints the first entry of the list. I am just learning Python so I am not sure the best way to complete this task without having extra code or redundant code. I want to keep this as clean as possible. I already used the above core code to talk to another API that is a dict and starts with results: = so it was quite easy to pull the data I want, but this is the first API JSON list i have tried using so I need a little help so I can learn from this challenge.

I did try: python getting a list of value from list of dict

But the all lists are in the code itself and not a list being pulled from an API. As another option i moved away from requests to pycurl and now when checking type it shows as:

<class 'bytes'>

After much more research i found a solution and posted my new working code below as a answer.

GotCrypto
  • 3
  • 5

3 Answers3

0

Think of data as a list of dicts. In each dict, you would like to retrieve only two { key: value } pairs.

Thus what you can do is for each element in data: get the two key,value pairs you want, and append them to a a new list.

i.e.

    parsed_data = []
    for _dict in data: 
        new_dict = { "symbol": _dict["symbol"], "last": _dict["last"] }
        parsed_data.append(new_dict) 

That should return you the data as you've described you want it.

Tare Gaskin
  • 1,209
  • 2
  • 10
  • 13
  • problem is when I run print keys(yes i know this is not the exact print) i get no keys since it is a list – GotCrypto Mar 13 '19 at 01:05
  • i am guessing you used the data snippet and put it into a list vs what the code does is pull from the API as i get the following using your answer: raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not Response – GotCrypto Mar 13 '19 at 01:17
  • building on your idea, how would i get data(API pulled info) into the parsed data dict? Maybe that would work? Dump the API JSON list into a dict and then parse? That was part of my OP idea. Turn the pulled API JSON list into a dict then parse the dict. – GotCrypto Mar 13 '19 at 01:23
  • this was really close, i found a tutorial after much googling and for the first time in 2 hours I got all the symbols to print out in a list – GotCrypto Mar 13 '19 at 03:00
0

Iterate over the list of dictionaries in data list getting the dictionary value by key:

print([['symbol:', d.get('symbol'), 'last:', d.get('last')] for d in data])

or indexing with key:

print([['symbol:', d['symbol'], 'last:', d['last']] for d in data])

both produce same result:

[['symbol:', 'BCNBTC', 'last:', '0.00000019437'], ['symbol:', 'BTCUSD', 'last:', '3886.76'], ['symbol:', 'DASHBTC', 'last:', '0.023404'], ['symbol:', 'DOGEBTC', 'last:', '0.00000051456'], ['symbol:', 'DOGEUSD', 'last:', '0.00199604'], ['symbol:', 'EMCBTC', 'last:', '0.000073615'], ['symbol:', 'ETHBTC', 'last:', '0.034325'], ['symbol:', 'FCNBTC', 'last:', '0.000011621']]

As suggested in python getting a list of value from list of dict, if a value is missing, add an 'alternate' value to fill it:

print([['symbol:', d.get('symbol', None), 'last:', d.get('last', None)] for d in data if ('symbol' in d and 'last' in d)])
  • i am guessing you used the list snippet and put it into a list vs me pulling the data from the API as using any of your options, when running the code i get: raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not Response – GotCrypto Mar 13 '19 at 01:11
  • yes sorry about the confusion, I cannot reproduce your API response – chickity china chinese chicken Mar 13 '19 at 01:50
  • i found 3 ways to change the class if that helps, i got byte, str, and list, maybe one is easier then the others, i updated my OP – GotCrypto Mar 13 '19 at 02:25
  • honestly the exchange that made this API is not easy to work with, another site connects to their API and makes it a dict, but they do not give a link to all coins, you have to add the coin pair at the end of the address – GotCrypto Mar 13 '19 at 02:27
0

After hours of trying and tinkering and googling i found someone with the same issue, different code, similar API JSON list format and once adapting part of their code to mine i got just the symbols to print from the list and only the symbols so from here I can make some minor adjustments to print just the symbols and last and a little indenting make it pretty. They did something very close to my code, but dumped their API into a file first so please let me know if this is a better way then below:

def pull():
    r = requests.get('API_ADDRESS')
    data = r.json()
    # use to confirm class type
    #print(type(data))
    # use to see if keys are present
    #print(data.keys())
    items = []
    for item in data:
        items.append(item['symbol'])
    print(items)
GotCrypto
  • 3
  • 5