bit lost here... trying to iterate through this array in a json object:
{
"NULSBUSD": {
"symbol": "NULSBUSD",
"orderId": 33523092,
"orderListId": -1,
"clientOrderId": "54Re4e4iV0bCkIXKyth4Sc",
"transactTime": 1659875121897,
"price": "0.00000000",
"origQty": "187.00000000",
"executedQty": "187.00000000",
"cummulativeQuoteQty": "50.10100000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "MARKET",
"side": "BUY",
"fills": [
{
"price": "0.26790000",
"qty": "150.00000000",
"commission": "0.00009529",
"commissionAsset": "BNB",
"tradeId": 669893
},
{
"price": "0.26800000",
"qty": "37.00000000",
"commission": "0.00002350",
"commissionAsset": "BNB",
"tradeId": 669894
}
],
"delta": 0,
"tsp": 0.264528
}
}
this code throws
string indices must be integers
qty = 0.0
for coin in order:
for fill in coin['fills']:
qty = float(fill['qty'])
Any ideas how I go about it? Thanks!
CodePudding user response:
This is what you need:
qty = 0.0
for coin in order:
obj = order[coin]
for fill in obj['fills']:
qty = float(fill['qty'])
print(qty)
You can do this in one line:
print(sum(float(fill['qty']) for coin in order for fill in order[coin]['fills']))
Output:
187.0
If you do: for key in order, key is a string here:)
CodePudding user response:
Here's using dataframe to avoid looping -
import pandas as pd
import json
with open("data.json") as f:
json_data = json.load(f)
for coin in json_data:
df = pd.DataFrame(json_data[coin]["fills"])
df["qty"].astype("float").sum()