Linked Questions

Popular Questions

Removing Specific Lines in Json File

Asked by At

I am trying to clean the json file down below. I want to remove all the dict key value pairs for which the key is "Company" in the "Stores" list.

{
"Company": "Apple",
"Stores": [
    {"Company" : "Apple",
     "Location" : "-",
     "Sales": "-",
     "Total_Employees": "-"
     },
    {"Company" : "Apple",
     "Location" : "-",
     "Sales": "-",
     "Total_Employees"
     },
    {"Company" : "Apple",
     "Location" : "-",
     "Sales": "-",
     "Total_Employees": "-"
     },    
]

}

This is my code so far:

import json

with open("stores.json", 'r') as jf:
    jsonFile = json.load(jf)

print(len(jsonFile))
testJson = {}

for k, v in jsonFile.items():
    for key in v:
        if not key.startswith('Company'):
            print(key)
            testJson = jsonFile[key]

print(len(jsonFile))

When I run it im getting TypeError: 'int' object is not iterable.

Related Questions