I have a large JSON file that needs cutting, I’m trying to delete the following items: "owner", "ticker", "comment" and "ptr_link" as keys.
JSON file:
{
"transactions": {
"0": [
{
"transaction_date": "11/29/2022",
"owner": "Spouse",
"ticker": "<a href="https://finance.yahoo.com/q?s=WIW" target="_blank">WIW</a>",
"asset_description": "Western Asset Inflation-Linked Opportunities & Inc",
"asset_type": "Stock",
"type": "Sale (Full)",
"amount": "$1,001 - $15,000",
"comment": "--",
"ptr_link": "https://efdsearch.senate.gov/search/view/ptr/5ac4d053-0258-4531-af39-8a8067f0d085/"
},
{
"transaction_date": "11/29/2022",
"owner": "Spouse",
"ticker": "<a href="https://finance.yahoo.com/q?s=GBIL" target="_blank">GBIL</a>",
"asset_description": "Goldman Sachs Access Treasury 0-1 Year ETF",
"asset_type": "Other Securities",
"type": "Purchase",
"amount": "$1,001 - $15,000",
"comment": "--",
"ptr_link": "https://efdsearch.senate.gov/search/view/ptr/5ac4d053-0258-4531-af39-8a8067f0d085/"
}
]
}
}
The "0" that holds this list can range upto the 60’s so I need to collectively access all of them rather than writing for specifically this list. The same applies for the dictionaries that hold the keys/values, as there could be numerous amounts, so I can’t input [0] or [1] etc.
this is my code, I’m trying to filter to the according object and simply delete the keys. Although I need to do this collectively as mentioned.
import json
data = json.load(open("xxxtester.json"))
data1 = data['transactions']
data2 = data1['0'][0]
for i in data2:
del data2['owner']
for i in data2:
del data2['ticker']
for i in data2:
del data2['comment']
for i in data2:
del data2['ptr_link']
open("xxxtester.json", "w").write(json.dumps(data, indent=4))
2
Answers
If you just want to remove some keys from each dictionary in list lets try this
Try:
Prints:
To save back as Json: