pythonjsonpython-requestsfindcrypto.com-exchange-api

How to find and use certain result from requests.post json response?


I am new to python, but self learnt myself to create crypto.com trading bot and got it to work. I have never coded before so this was really interesting experience :)

However, one problem I ran into was how to find certain string from requests.post json response and use it to print data from only that row? Currently im doing it like this:

balance = requests.post(url=URL + "/v1/account", data=params, json=json)


this is what I get:

 {'code': '0', 'msg': 'suc', 'data': {'total_asset': '0', 'coin_list': [{'normal': '0.000000000000000000', 'locked': '0.000000000000000000', 'coin': 'ENJ'}, {'normal': '1506.701942200000000000', 'locked': '0.000000000000000000', 'coin': 'CRO'}, {'normal': '0', 'locked': '0', 'coin': 'KNC'}, {'normal': '0', 'locked': '0', 'coin': 'VET'}, {'normal': '0.000059800000000000', 'locked': '0.000000000000000000', 'coin': 'PAXG'}, {'normal': '0.000096800000000000', 'locked': '0.000000000000000000', 'coin': 'USDT'}, {'normal': '0', 'locked': '0', 'coin': 'ALGO'}, {'normal': '0', 'locked': '0', 'coin': 'ATOM'}, {'normal': '0', 'locked': '0', 'coin': 'XRP'}, {'normal': '0', 'locked': '0', 'coin': 'XLM'}, {'normal': '0', 'locked': '0', 'coin': 'LINK'}, {'normal': '0', 'locked': '0', 'coin': 'XTZ'}, {'normal': '0', 'locked': '0', 'coin': 'BCH'}, {'normal': '0', 'locked': '0', 'coin': 'EOS'}, {'normal': '0', 'locked': '0', 'coin': 'DAI'}, {'normal': '0.000000000000000000', 'locked': '0', 'coin': 'BTC'}, {'normal': '0.003788320000000000', 'locked': '0.000000000000000000', 'coin': 'BAT'}, {'normal': '0', 'locked': '0', 'coin': 'ETC'}, {'normal': '0.014441820000000000', 'locked': '0.000000000000000000', 'coin': 'ETH'}, {'normal': '0', 'locked': '0', 'coin': 'MCO'}, {'normal': '0.004229970000000000', 'locked': '0.000000000000000000', 'coin': 'NEO'}, {'normal': '0', 'locked': '0', 'coin': 'LTC'}, {'normal': '0', 'locked': '0', 'coin': 'USDC'}, {'normal': '0', 'locked': '0', 'coin': 'ADA'}, {'normal': '0', 'locked': '0', 'coin': 'ICX'}]}}


Then I get coins that I want to trade like this:

balance1 = balance.json()
balance2 = balance1.get('data')
balance3 = balance2.get('coin_list')
balanceCRO = balance3[1]
balanceETH = balance3[19]
CROopen = balanceCRO.get('normal')
CROlocked = balanceCRO.get('locked')
ETHopen = balanceETH.get('normal')
ETHlocked = balanceETH.get('locked')

This works the way I want it to work, but when they add new coins, the positions of CRO and ETH change, so I have to manually change the balance[1], balance[19] to the corresponding numbers every time they do.

Is there a way to find certain string (in this case CRO and ETH) from the response list and use it to get values from those?

Sorry if I explained it badly, I am still learning :) Thanks in advance!


Solution

  • Here's a way to sort your data in a way that would not be impacted by addition or deletion:

    response = {'code': '0', 'msg': 'suc', 'data': {'total_asset': '0', 'coin_list': [{'normal': '0.000000000000000000', 'locked': '0.000000000000000000', 'coin': 'ENJ'}, {'normal': '1506.701942200000000000', 'locked': '0.000000000000000000', 'coin': 'CRO'}, {'normal': '0', 'locked': '0', 'coin': 'KNC'}, {'normal': '0', 'locked': '0', 'coin': 'VET'}, {'normal': '0.000059800000000000', 'locked': '0.000000000000000000', 'coin': 'PAXG'}, {'normal': '0.000096800000000000', 'locked': '0.000000000000000000', 'coin': 'USDT'}, {'normal': '0', 'locked': '0', 'coin': 'ALGO'}, {'normal': '0', 'locked': '0', 'coin': 'ATOM'}, {'normal': '0', 'locked': '0', 'coin': 'XRP'}, {'normal': '0', 'locked': '0', 'coin': 'XLM'}, {'normal': '0', 'locked': '0', 'coin': 'LINK'}, {'normal': '0', 'locked': '0', 'coin': 'XTZ'}, {'normal': '0', 'locked': '0', 'coin': 'BCH'}, {'normal': '0', 'locked': '0', 'coin': 'EOS'}, {'normal': '0', 'locked': '0', 'coin': 'DAI'}, {'normal': '0.000000000000000000', 'locked': '0', 'coin': 'BTC'}, {'normal': '0.003788320000000000', 'locked': '0.000000000000000000', 'coin': 'BAT'}, {'normal': '0', 'locked': '0', 'coin': 'ETC'}, {'normal': '0.014441820000000000', 'locked': '0.000000000000000000', 'coin': 'ETH'}, {'normal': '0', 'locked': '0', 'coin': 'MCO'}, {'normal': '0.004229970000000000', 'locked': '0.000000000000000000', 'coin': 'NEO'}, {'normal': '0', 'locked': '0', 'coin': 'LTC'}, {'normal': '0', 'locked': '0', 'coin': 'USDC'}, {'normal': '0', 'locked': '0', 'coin': 'ADA'}, {'normal': '0', 'locked': '0', 'coin': 'ICX'}]}}
    data = response["data"]
    coins = data["coin_list"]
    coins_dict = {}
    for coin in coins:
        coins_dict[coin["coin"]] = coin
        
    print(coins_dict["CRO"])
    

    This outputs:

    {'normal': '1506.701942200000000000',
     'locked': '0.000000000000000000',
     'coin': 'CRO'}
    

    which I believe is what balanceCRO represents in your original code.