Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to write a for loop that goes through the assets (BTC, EOS) and the available_balance (0.00087168, 0). The assets work however I cannot get to the available_balance. How would I be able to get to it with json.

import json 

pairs= '''
({
  'ext_code': '',
  'rate_limit_status': 118,
  'result': {
      'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0},
      'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0}
  },
  'ret_code': 0,
  'ret_msg': 'OK',
  'time_now': '1619987733.732306'}
<bravado.requests_client.RequestsResponseAdapter object at 0x000002D6F7FEB808>)
'''
data = json.loads(pairs)
Wallet_balance = data[0]['result']
for assets in balance:
    print("Asset: ", assets, " Balance: ", assets['available_balance'])

Expected Output:

Asset: BTC Balance: 0.00087168
Asset: EOS Balance: 0

The pprint Output:

({'ext_code': '',
  'ext_info': '',
  'rate_limit': 120,
  'rate_limit_reset_ms': 1620081183677,
  'rate_limit_status': 117,
  'result': {'BTC': {'available_balance': 0.00087168,
                     'cum_realised_pnl': 7.288e-05,
                     'equity': 0.00087168,
                     'given_cash': 0,
                     'occ_closing_fee': 0,
                     'occ_funding_fee': 0,
                     'order_margin': 0,
                     'position_margin': 0,
                     'realised_pnl': 0,
                     'service_cash': 0,
                     'unrealised_pnl': 0,
                     'used_margin': 0,
                     'wallet_balance': 0.00087168},
             'EOS': {'available_balance': 0,
                     'cum_realised_pnl': 0,
                     'equity': 0,
                     'given_cash': 0,
                     'occ_closing_fee': 0,
                     'occ_funding_fee': 0,
                     'order_margin': 0,
                     'position_margin': 0,
                     'realised_pnl': 0,
                     'service_cash': 0,
                     'unrealised_pnl': 0,
                     'used_margin': 0,
                     'wallet_balance': 0},
             'ETH': {'available_balance': 0.03362706,
                     'cum_realised_pnl': -7.41e-06,
                     'equity': 0.03362706,
                     'given_cash': 0,
                     'occ_closing_fee': 0,
                     'occ_funding_fee': 0,
                     'order_margin': 0,
                     'position_margin': 0,
                     'realised_pnl': 0,
                     'service_cash': 0,
                     'unrealised_pnl': 0,
                     'used_margin': 0,
                     'wallet_balance': 0.03362706},
             'USDT': {'available_balance': 0,
                      'cum_realised_pnl': 0,
                      'equity': 0,
                      'given_cash': 0,
                      'occ_closing_fee': 0,
                      'occ_funding_fee': 0,
                      'order_margin': 0,
                      'position_margin': 0,
                      'realised_pnl': 0,
                      'service_cash': 0,
                      'unrealised_pnl': 0,
                      'used_margin': 0,
                      'wallet_balance': 0},
             'XRP': {'available_balance': 0,
                     'cum_realised_pnl': 0,
                     'equity': 0,
                     'given_cash': 0,
                     'occ_closing_fee': 0,
                     'occ_funding_fee': 0,
                     'order_margin': 0,
                     'position_margin': 0,
                     'realised_pnl': 0,
                     'service_cash': 0,
                     'unrealised_pnl': 0,
                     'used_margin': 0,
                     'wallet_balance': 0}},
  'ret_code': 0,
  'ret_msg': 'OK',
  'time_now': '1620081183.700541'},
 <bravado.requests_client.RequestsResponseAdapter object at 0x0000016DC011F888>)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
144 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just a heads up, your json is not valid. In addition to using single quotes, it is just missing parts.

Assuming you had either of the following, then this should get you going:

import json
import ast

## -----------------------------
## I have valid json
## -----------------------------
pairs_double_quoted = '''
[{
  "ext_code": "",
  "rate_limit_status": 118,
  "result": {
      "BTC": {"available_balance": 0.00087168, "cum_realised_pnl": 7.288e-05, "equity": 0.00087168, "given_cash": 0},
      "EOS": {"available_balance": 0, "cum_realised_pnl": 0, "equity": 0, "given_cash": 0}
  },
  "ret_code": 0,
  "ret_msg": "OK",
  "time_now": "1619987733.732306"
}]
'''
## -----------------------------

## -----------------------------
## I have something else that still looks hopeful
## -----------------------------
pairs_single_quoted = '''
({
  'ext_code': '',
  'rate_limit_status': 118,
  'result': {
      'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0},
      'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0}
  },
  'ret_code': 0,
  'ret_msg': 'OK',
  'time_now': '1619987733.732306'
})
'''
## -----------------------------

## -----------------------------
## one or the other depending on if you have proper json (double) quotes
## -----------------------------
data = json.loads(pairs_double_quoted)
#data = [ast.literal_eval(pairs_single_quoted)]
## -----------------------------

## -----------------------------
## data is a list so we likely want to go though each item on the list
## if you only want the first item then use
## data_item = data[0]
## -----------------------------
for data_item in data:
    ## -----------------------------
    ## for each key value pair in the result dict print something
    ## -----------------------------
    for asset_key, asset_value in data_item["result"].items():
        print("Asset: {} Balance: {}".format(asset_key, asset_value["available_balance"]))
    ## -----------------------------
## -----------------------------

Based on your update that the data you have is exactly:

pairs_single_quoted = '''
({
  'ext_code': '',
  'rate_limit_status': 118,
  'result': {
      'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0},
      'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0}
  },
  'ret_code': 0,
  'ret_msg': 'OK',
  'time_now': '1619987733.732306'
}<bravado.requests_client.RequestsResponseAdapter object at 0x000002D6F7FEB808>)
'''

for some reason, then you likely want to go the ast route:

import re
import ast

## -----------------------------
## I have something else that still looks hopeful
## -----------------------------
pairs_single_quoted = '''
({
    'ext_code': '',
    'ext_info': '',
    'rate_limit': 120,
    'rate_limit_reset_ms': 1620081183677,
    'rate_limit_status': 117,
    'result': {
        'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'order_margin': 0, 'position_margin': 0, 'realised_pnl': 0, 'service_cash': 0, 'unrealised_pnl': 0, 'used_margin': 0, 'wallet_balance': 0.00087168},
        'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'order_margin': 0, 'position_margin': 0, 'realised_pnl': 0, 'service_cash': 0, 'unrealised_pnl': 0, 'used_margin': 0, 'wallet_balance': 0},
        'ETH': {'available_balance': 0.03362706, 'cum_realised_pnl': -7.41e-06, 'equity': 0.03362706, 'given_cash': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'order_margin': 0, 'position_margin': 0, 'realised_pnl': 0, 'service_cash': 0, 'unrealised_pnl': 0, 'used_margin': 0, 'wallet_balance': 0.03362706}
    },
    'ret_code': 0,
    'ret_msg': 'OK',
    'time_now': '1620081183.700541'},
    <bravado.requests_client.RequestsResponseAdapter object at 0x0000016DC011F888>)
 '''
## -----------------------------

## -----------------------------
## a regex wizard would do this via re.sub()
## -----------------------------
pattern = re.compile(r"^(.*),.*<bravado.*$", re.DOTALL)
text_in = re.findall(pattern, pairs_single_quoted)[0] + ")"
data = [ast.literal_eval(text_in)]
## -----------------------------

## -----------------------------
## data is a list so we likely want to go though each item on the list
## if you only want the first item then use
## data_item = data[0]
## -----------------------------
for data_item in data:
    ## -----------------------------
    ## for each key value pair in the result dict print something
    ## -----------------------------
    for asset_key, asset_value in data_item["result"].items():
        print("Asset: {} Balance: {}".format(asset_key, asset_value["available_balance"]))
    ## -----------------------------
## -----------------------------

This should result in:

Asset: BTC Balance: 0.00087168
Asset: EOS Balance: 0
Asset: ETH Balance: 0.03362706

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...