首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤JSON输出

过滤JSON输出
EN

Stack Overflow用户
提问于 2022-01-31 10:00:36
回答 2查看 60关注 0票数 0

我想过滤一个JSON输出,但是它给了我以下错误:

代码语言:javascript
复制
File "main.py", line 40, in on_ready
output_dict = [x for x in a2 if x['type'] == 'ORDER_FILL']
File "main.py", line 40, in <listcomp>
output_dict = [x for x in a2 if x['type'] == 'ORDER_FILL']
TypeError: string indices must be integers

这是我的密码:

代码语言:javascript
复制
json_output = {'transactions': [{'id': '111', 'accountID': 'xxxxx', 'userID': xxxxx, 'batchID': '109', 'time': '2022-01-31T00:50:29.592777450Z', 'type': 'STOP_LOSS_ORDER', 'tradeID': '109', 'timeInForce': 'GTC', 'triggerCondition': 'DEFAULT', 'triggerMode': 'TOP_OF_BOOK', 'price': '15140.9', 'reason': 'ON_FILL'}, {'id': '112', 'accountID': 'xxxxx', 'userID': xxxxx, 'batchID': '112', 'requestID': '24908231217089037', 'time': '2022-01-31T08:29:58.903631976Z', 'type': 'MARKET_ORDER', 'instrument': 'DE30_EUR', 'units': '-0.1', 'timeInForce': 'FOK', 'positionFill': 'DEFAULT', 'reason': 'CLIENT_ORDER'}, {'id': '113', 'accountID': 'xxxxx', 'userID': xxxxx, 'batchID': '112', 'requestID': '24908231217089037', 'time': '2022-01-31T08:29:58.903631976Z', 'type': 'ORDER_FILL', 'orderID': '112', 'instrument': 'DE30_EUR', 'units': '-0.1', 'requestedUnits': '-0.1', 'price': '15499.6', 'pl': '5.9200', 'quotePL': '5.92', 'financing': '0.0000', 'baseFinancing': '0.00000000000000', 'commission': '0.0000', 'accountBalance': '525.0693', 'gainQuoteHomeConversionFactor': '1', 'lossQuoteHomeConversionFactor': '1', 'guaranteedExecutionFee': '0.0000', 'quoteGuaranteedExecutionFee': '0', 'halfSpreadCost': '0.0850', 'fullVWAP': '15499.6', 'reason': 'MARKET_ORDER', 'tradesClosed': [{'tradeID': '109', 'units': '-0.1', 'realizedPL': '5.9200', 'financing': '0.0000', 'baseFinancing': '0.00000000000000', 'price': '15499.6', 'guaranteedExecutionFee': '0.0000', 'quoteGuaranteedExecutionFee': '0', 'halfSpreadCost': '0.0850', 'plHomeConversionCost': '0.00', 'baseFinancingHomeConversionCost': '0.00000000000000', 'guaranteedExecutionFeeHomeConversionCost': '0', 'homeConversionCost': '0.00000000000000'}], 'fullPrice': {'closeoutBid': '15496.4', 'closeoutAsk': '15504.5', 'timestamp': '2022-01-31T08:29:58.871175423Z', 'bids': [{'price': '15499.6', 'liquidity': '100'}, {'price': '15499.1', 'liquidity': '100'}, {'price': '15496.4', 'liquidity': '100'}], 'asks': [{'price': '15501.3', 'liquidity': '100'}, {'price': '15501.9', 'liquidity': '100'}, {'price': '15504.5', 'liquidity': '100'}]}, 'homeConversionFactors': {'gainQuoteHome': {'factor': '1'}, 'lossQuoteHome': {'factor': '1'}, 'gainBaseHome': {'factor': '15422.8980'}, 'lossBaseHome': {'factor': '15577.9020'}}, 'plHomeConversionCost': '0.00', 'baseFinancingHomeConversionCost': '0.00000000000000', 'guaranteedExecutionFeeHomeConversionCost': '0', 'homeConversionCost': '0.00000000000000'}, {'id': '114', 'accountID': 'xxxxx', 'userID': xxxxx, 'batchID': '112', 'requestID': '24908231217089037', 'time': '2022-01-31T08:29:58.903631976Z', 'type': 'ORDER_CANCEL', 'orderID': '110', 'reason': 'LINKED_TRADE_CLOSED', 'closedTradeID': '109', 'tradeCloseTransactionID': '113'}, {'id': '115', 'accountID': 'xxxxx', 'userID': xxxxx, 'batchID': '112', 'requestID': '24908231217089037', 'time': '2022-01-31T08:29:58.903631976Z', 'type': 'ORDER_CANCEL', 'orderID': '111', 'reason': 'LINKED_TRADE_CLOSED', 'closedTradeID': '109', 'tradeCloseTransactionID': '113'}], 'lastTransactionID': '115'}
        
input_dict = json_output
output_dict = [x for x in input_dict if x['type'] == 'ORDER_FILL'] # Filter if type is this
output_json = json.dumps(output_dict)
print(output_json)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-31 10:11:05

我将详细谈谈RJ .阿德里亚森的评论。

您的变量json_output是一个字典。当迭代字典时,您将得到字典键(在本例中是transactionlastTransactionID)。

您想要的是迭代根字典中transaction键中嵌套的列表。你可以这样做:

代码语言:javascript
复制
[x for x in json_output['transactions'] if x['type'] == 'ORDER_FILL']
票数 2
EN

Stack Overflow用户

发布于 2022-01-31 10:10:52

您必须在事务列表中循环,而不是在dict中循环:

代码语言:javascript
复制
import json
...
input_dict = json_output
input_dict["transactions"] = [x for x in input_dict["transactions"] if x['type'] == 'ORDER_FILL'] # Filter if type is this
output_json = json.dumps(input_dict, indent=4)
print(output_json)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70924087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档