1

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))
2
  • Are you trying to delete the items that have an owner, ticker, comment and ptr_link, or simply delete those attributes from the item? You seem to be saying the former but doing the latter. Commented Dec 12, 2022 at 14:35
  • Just simply delete them as all objects hold these items/keys. I will edit the question now thank you Commented Dec 12, 2022 at 14:44

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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))

2 Comments

This gives me the error: ["'list' object has no attribute 'values'"] on the first line of the for loop
Sorry @kie_codes . Fixed, now should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.