jsonpython-3.xapirestkraken.com

Python last index (not per number)


I'm trying to read the TradesHistory from krakenn but it doesn't work with the index. It only works if I enter the right trade. How can I step through each individual trade individually?

With the following command I get all the trades. It works.

print(resp.json()['result']['trades'])

If I wanted to access the first index now, it doesn't work.

print(resp.json()['result']['trades'][0]['ordertxid'])

Only after entering the correct trade id can I access the following index.

print(resp.json()['result']['trades']['12345-abcde-ZYXWE']['ordertxid'])

What am I doing wrong? How can I access the correct index without first knowing the ID?

{
   "error":[
      
   ],
   "result":{
      "count":2,
      "trades":{
         "12345-abcde-ZYXWE":{},
         "ZYXWE-12345-abcde":{
            "ordertxid":"xyz",
         }
      }
   }
}

various Python index commands with different JSON formats


Solution

  • In order to go over all the trades, you can loop over the trades that were made and extract their information.

    You can use a for loop like this:

    trades = resp.json()['result']['trades']
    for trade in trades: # accessing each trades
        if trades[trade] == {}:
            print('trade {} is empty.'.format(trade))
            continue
        print('{}\'s trade information:'.format(trade))
        for value in trades[trade]: # accessing each value inside the trade dictionary
            print(value, ':', trades[trade][value])