I am trying to loop through a list of objects deleting an element from each object. Each object is a new line. I am trying to then save the new file as is without the element contained within the objects.
{
"business_id": "fNGIbpazjTRdXgwRY_NIXA",
"full_address": "1201 Washington Ave\nCarnegie, PA 15106",
"hours": {
"Monday": {
"close": "23:00",
"open": "11:00"
},
"Tuesday": {
"close": "23:00",
"open": "11:00"
},
"Friday": {
"close": "23:00",
"open": "11:00"
},
"Wednesday": {
"close": "23:00",
"open": "11:00"
},
"Thursday": {
"close": "23:00",
"open": "11:00"
},
"Saturday": {
"close": "23:00",
"open": "11:00"
}
},
"open": true,
"categories": ["Bars", "American (Traditional)", "Nightlife", "Lounges", "Restaurants"],
"city": "Carnegie",
"review_count": 7,
"name": "Rocky's Lounge",
"neighborhoods": [],
"longitude": -80.0849416,
"state": "PA",
"stars": 4.0,
"latitude": 40.3964688,
"attributes": {
"Alcohol": "full_bar",
"Noise Level": "average",
"Music": {
"dj": false
},
"Attire": "casual",
"Ambience": {
"romantic": false,
"intimate": false,
"touristy": false,
"hipster": false,
"divey": false,
"classy": false,
"trendy": false,
"upscale": false,
"casual": false
},
"Good for Kids": true,
"Wheelchair Accessible": true,
"Good For Dancing": false,
"Delivery": false,
"Dogs Allowed": false,
"Coat Check": false,
"Smoking": "no",
"Accepts Credit Cards": true,
"Take-out": true,
"Price Range": 1,
"Outdoor Seating": false,
"Takes Reservations": false,
"Waiter Service": true,
"Wi-Fi": "free",
"Caters": false,
"Good For": {
"dessert": false,
"latenight": false,
"lunch": false,
"dinner": false,
"brunch": false,
"breakfast": false
},
"Parking": {
"garage": false,
"street": false,
"validated": false,
"lot": true,
"valet": false
},
"Has TV": true,
"Good For Groups": true
},
"type": "business"
}
I need to remove the information contained within the hours element, however the information is not always the same. Some contain all the days and some only contain one or two day information.
This is the code I've tried:
import json
with open('data.json') as data_file:
data = json.load(data_file)
for element in data:
del element['hours']
However, I am getting an error when running the code:
TypeError: 'str' object does not support item deletion