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.
2
Answers
First off, your json is wrong, it’s missing values for Total_employee
you can modify it the following way :
Then, to answer your first question
Having made changes to the data shown in the question (to make it valid JSON) you could do this: