Home > other >  Delete item from JSON based on key Python
Delete item from JSON based on key Python

Time:12-13

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 &amp; 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))

CodePudding user response:

Try:

import json

with open("your_data.json", "r") as f_in:
    data = json.load(f_in)

to_delete = {"owner", "ticker", "comment", "ptr_link"}

for k in data["transactions"]:
    data["transactions"][k] = [
        {kk: vv for kk, vv in d.items() if kk not in to_delete}
        for d in data["transactions"][k]
    ]

print(data)

Prints:

{
    "transactions": {
        "0": [
            {
                "transaction_date": "11/29/2022",
                "asset_description": "Western Asset Inflation-Linked Opportunities &amp; Inc",
                "asset_type": "Stock",
                "type": "Sale (Full)",
                "amount": "$1,001 - $15,000",
            },
            {
                "transaction_date": "11/29/2022",
                "asset_description": "Goldman Sachs Access Treasury 0-1 Year ETF",
                "asset_type": "Other Securities",
                "type": "Purchase",
                "amount": "$1,001 - $15,000",
            },
        ]
    }
}

To save back as Json:

with open("output.json", "w") as f_out:
    json.dump(data, f_out, indent=4)

CodePudding user response:

If you just want to remove some keys from each dictionary in list lets try this

data = json.load(open("xxxtester.json"))
for_delete = ["owner", "ticker", "comment", "ptr_link"]

for d in data['transactions']['0']:
    for key in for_delete:
        if key in d:
            d.pop(key)


open("xxxtester.json", "w").write(
    json.dumps(data, indent=4))
  • Related