Search code examples
pythonarraysjsonbinance

python array dictionary inside dictionary


I have python dictionary the code:

{'code': 200,
 'msg': '',
 'snapshotVos': [{'data': {'balances': [{'asset': 'ADD',
                                         'free': '10',
                                         'locked': '0'},
                                        {'asset': 'RDP',
                                         'free': '0',
                                         'locked': '0'},
                                        {'asset': 'SHIB',
                                         'free': '0',
                                         'locked': '947415'}],
                           'totalAsset': '152'},
                  'type': 'spot',
                  'updateTime': 1703807999000}]}

want to take the object values from 'balances'

I try code but just able to give the 'snapshotVos' and 'data'

I need result for all value in array 'balances'.

need keys from 'asset', 'free', 'locked' and if any array has 0 in the 'free' and 'locked' remove this array.

print(repo['snapshotVos'])
rows = []
for re in repo['snapshotVos']:
    typeOrder = re['data']
    rows.append(typeOrder)
print(rows)
rows1 = []
for re1 in rows:
    typeOrder = re1['balances'][0]
    rows1.append(typeOrder)
print(rows1)

the result

>>> 200
>>> [{'type': 'spot', 'updateTime': 1703807999000, 'data': {'totalAssetOfBtc': '0.00021152', 'balances': [{'asset': 'ADD', 'free': '10', 'locked': '0'}, {'asset': 'ADXOLD', 'free': '0', 'locked': '0'}]}}]
>>> [{'totalAssetOfBtc': '0.00021152', 'balances': [{'asset': 'ADD', 'free': '10', 'locked': '0'}, {'asset': 'ADXOLD', 'free': '0', 'locked': '0'}]}]
>>>  [{'asset': 'ADD', 'free': '10', 'locked': '0'}]

Solution

  • Add another nested loop:

    for re in repo['snapshotVos']:
        for balance in re['data']['balances']:
            if not (balance['free'] == '0' and balance['locked'] == '0'):
                val = list(balance.values())
                if val not in rows1:
                    rows1.append(val)