Skip to content
Advertisement

trying to get specific data from API response

Trying to get "equity" data from a API response for calculate but can’t get through.

This is my code:

from pybit import HTTP
import json


session = HTTP("https://api.bybit.com",api_key=xxxx, api_secret=xxxx)
g=session.get_wallet_balance(coin="USDT")

data=json.dumps(g,indent=4)
print(data)

for item in data ['result']['USDT']:
    print(item["equity"])

Keep getting TypeError:

    for item in data ['result']['USDT']:
TypeError: string indices must be integers

And this is data I am trying to tear apart:

{
    "ret_code": 0,
    "ret_msg": "OK",
    "ext_code": "",
    "ext_info": "",
    "result": {
        "USDT": {
            "equity": 562733.1693,
            "available_balance": 558590.8075,
            "used_margin": 5642.0618,
            "order_margin": 0,
            "position_margin": 05642.0618,
            "occ_closing_fee": 3.8e-07,
            "occ_funding_fee": 0,
            "wallet_balance": 564232.8693,
            "realised_pnl": -00862.2551,
            "unrealised_pnl": -01.4997,
            "cum_realised_pnl": 68531.4329,
            "given_cash": 0,
            "service_cash": 0
        }
    },
    "time_now": "1643729725.986204",
    "rate_limit_status": 119,
    "rate_limit_reset_ms": 1643729725980,
    "rate_limit": 120
}

Advertisement

Answer

Done with this type, go through json.dumps and json.loads and track the data in equity to get number in json

session = HTTP("https://api.bybit.com",api_key=apiKey, api_secret=secret)
raw= json.dumps(session.get_wallet_balance(coin="USDT"),indent=4)
data=json.loads(raw)
equity=data['result']['USDT']['equity']
print(equity)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement