Reputation: 761
I want to filter a JSON output but it gives me the following error:
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
This is my code:
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)
Upvotes: 0
Views: 98
Reputation: 590
I will expand on RJ Adriaansen's comment.
Your variable json_output
is a dictionary. When iterating through dictionary, you are getting dictionary keys (in this case transaction
and lastTransactionID
).
What you want is to iterate through the list nested within transaction
key in the root dictionary. You can do that like this:
[x for x in json_output['transactions'] if x['type'] == 'ORDER_FILL']
Upvotes: 2
Reputation: 1087
You have to cycle trough the transaction list not the dict:
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)
Upvotes: 0