Home > Enterprise >  Python - Extract/access information from json via Kraken API
Python - Extract/access information from json via Kraken API

Time:10-31

I'm trying to understand how to work with the json returns I am getting from de Kraken API.

This is my code:

resp = kraken_request("/0/private/TradeBalance", {
    "nonce": str(int(10000*time.time())),
    "asset": "USD"
}, api_key, api_sec)

print("TradeBalance: "   str(resp.json()))

resp = kraken_request("/0/private/OpenOrders", {
    "nonce": str(int(10000*time.time())),
    "asset": "USD"
}, api_key, api_sec)
print()
print("OpenOrders: "   str(resp.json())

This is my json return (edited so it is no real data):

{"error":[],"result":{"open":{"ABCDE-AWSCG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683580.8848298,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.70000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.70000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-AHKEK-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683548.8137474,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.60000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.60000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-POQX7-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683517.207053,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.50000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.50000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-UADOV-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683490.4299977,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.40000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.40000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-ABCDE-2JGH3M":{"refid":null,"userref":0,"status":"open","opentm":1666646615.668582,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.30000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.30000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-623OO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646572.9639573,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.20000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.20000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-4EHBG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646537.55891,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.10000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.10000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-SPHMF-ABCDE":{"refid":null,"link_id":"ABCDE-YWJVW-ABCDE","userref":0,"status":"open","opentm":1666646453.770994,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"7.99900","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 7.99900","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-RVWQO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1663311467.0131989,"starttm":0,"expiretm":0,"descr":{"pair":"ETHWUSD","type":"sell","ordertype":"limit","price":"1000.000","price2":"0","leverage":"none","order":"sell 357.00000000 ETHWUSD @ limit 1000.000","close":""},"vol":"357.00000000","vol_exec":"0.00000000","cost":"0.0000000","fee":"0.0000000","price":"0.0000000","stopprice":"0.0000000","limitprice":"0.0000000","misc":"","oflags":"fcib"}}}}

Now I can't work out how to work with this json, I'd like to go through it and deal with each of the items, like "ABCDE-AWSCG-ABCDE" and "ABCDE-POQX7-ABCDE" etc., with for each all of it's underlying information, I have no idea how to do this. Please note, I do not know the item names like "ABCDE-AWSCG-ABCDE" beforehand.

CodePudding user response:

You should use pandas. You can see all the data in a table. This is much easier than working with a json.

import pandas as pd
import numpy as np
null=np.nan

returned_json={"error":[],"result":{"open":{"ABCDE-AWSCG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683580.8848298,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.70000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.70000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-AHKEK-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683548.8137474,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.60000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.60000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-POQX7-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683517.207053,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.50000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.50000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-UADOV-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683490.4299977,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.40000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.40000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-ABCDE-2JGH3M":{"refid":null,"userref":0,"status":"open","opentm":1666646615.668582,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.30000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.30000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-623OO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646572.9639573,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.20000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.20000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-4EHBG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646537.55891,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.10000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.10000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-SPHMF-ABCDE":{"refid":null,"link_id":"ABCDE-YWJVW-ABCDE","userref":0,"status":"open","opentm":1666646453.770994,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"7.99900","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 7.99900","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-RVWQO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1663311467.0131989,"starttm":0,"expiretm":0,"descr":{"pair":"ETHWUSD","type":"sell","ordertype":"limit","price":"1000.000","price2":"0","leverage":"none","order":"sell 357.00000000 ETHWUSD @ limit 1000.000","close":""},"vol":"357.00000000","vol_exec":"0.00000000","cost":"0.0000000","fee":"0.0000000","price":"0.0000000","stopprice":"0.0000000","limitprice":"0.0000000","misc":"","oflags":"fcib"}}}}

df=pd.DataFrame(returned_json['result'])['open'].apply(pd.Series).rename(columns={'price':'price_main'})
df=df.join(df['descr'].apply(pd.Series))
print(df)
'''
                      refid userref status             opentm     starttm   expiretm    descr   vol vol_exec    cost    fee price_main  stopprice   limitprice  misc    oflags  link_id pair    type    ordertype   price   price2  leverage    order   close
ABCDE-4EHBG-ABCDE       0   open    1666646537.55891    0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.10000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.10000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fciq        LINKEUR sell    limit   8.10000 0   none    sell 1.00000000 LINKEUR @ limit 8.10000 
ABCDE-623OO-ABCDE       0   open    1666646572.9639573  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.20000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.20000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fciq        LINKEUR sell    limit   8.20000 0   none    sell 1.00000000 LINKEUR @ limit 8.20000 
ABCDE-ABCDE-2JGH3M      0   open    1666646615.668582   0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.30000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.30000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fciq        LINKEUR sell    limit   8.30000 0   none    sell 1.00000000 LINKEUR @ limit 8.30000 
ABCDE-AHKEK-ABCDE       0   open    1666683548.8137474  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.60000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.60000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.60000 0   none    sell 1.00000000 LINKEUR @ limit 8.60000 
ABCDE-AWSCG-ABCDE       0   open    1666683580.8848298  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.70000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.70000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.70000 0   none    sell 1.00000000 LINKEUR @ limit 8.70000 
ABCDE-POQX7-ABCDE       0   open    1666683517.207053   0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.50000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.50000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.50000 0   none    sell 1.00000000 LINKEUR @ limit 8.50000 
ABCDE-RVWQO-ABCDE       0   open    1663311467.0131989  0   0   {'pair': 'ETHWUSD', 'type': 'sell', 'ordertype': 'limit', 'price': '1000.000', 'price2': '0', 'leverage': 'none', 'order': 'sell 357.00000000 ETHWUSD @ limit 1000.000', 'close': ''}   357.00000000    0.00000000  0.0000000   0.0000000   0.0000000   0.0000000   0.0000000       fcib        ETHWUSD sell    limit   1000.000    0   none    sell 357.00000000 ETHWUSD @ limit 1000.000  
ABCDE-SPHMF-ABCDE       0   open    1666646453.770994   0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '7.99900', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 7.99900', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib    ABCDE-YWJVW-ABCDE   LINKEUR sell    limit   7.99900 0   none    sell 1.00000000 LINKEUR @ limit 7.99900 
ABCDE-UADOV-ABCDE       0   open    1666683490.4299977  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.40000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.40000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.40000 0   none    sell 1.00000000 LINKEUR @ limit 8.40000 


'''
  • Related