I am learning Python (teaching myself through articles and tutorials) and have come across something that I need assistance with. I have JSON data that represents TV listings, and I want to read it, edit it (remove outdated listings), and rewrite it back out. The sticking points for me are the nested data and how to iterate through/reference it to skip over the objects I don't want when writing out. Thanks. Here is some sample data:
{
"1": {
"channel_id": "1",
"img": "https://guide.tv/assets/images/channels/1.png",
"items": [
{
"category": "Comedy",
"channel": "1",
"description": "Latest episode of show A",
"end_time": "2017-09-11 20:30:00",
"language": "us",
"name": "Show A",
"quality": "720p",
"runtime": "30",
"time": "2017-09-11 20:00:00",
"version": "None"
},
{
"category": "Comedy",
"channel": "1",
"description": "Latest episode of show B",
"end_time": "2017-09-12 21:00:00",
"language": "us",
"name": "Show B",
"quality": "720p",
"runtime": "30",
"time": "2017-09-12 20:30:00",
"version": "None"
},
],
"name": "01 - NBC"
},
"2": {
"channel_id": "2",
"img": "https://guide.tv/assets/images/channels/2.png",
"items": [
{
"category": "Drama",
"channel": "2",
"description": "Latest episode of show C",
"end_time": "2017-09-10 23:00:00",
"language": "us",
"name": "Show C",
"quality": "720p",
"runtime": "180",
"time": "2017-09-10 20:00:00",
"version": "None"
},
{
"category": "Drama",
"channel": "2",
"description": "Latest episode of show D",
"end_time": "2017-09-11 23:00:00",
"language": "us",
"name": "Show D",
"quality": "720p",
"runtime": "60",
"time": "2017-09-11 22:00:00",
"version": "None"
},
{
"category": "Action",
"channel": "2",
"description": "Latest episode of Show E",
"end_time": "2017-09-11 22:00:00",
"language": "us",
"name": "Show E",
"quality": "720p",
"runtime": "180",
"time": "2017-09-11 19:00:00",
"version": "None"
},
{
"category": "Fiction",
"channel": "2",
"description": "Latest episode of show F",
"end_time": "2017-09-10 19:00:00",
"language": "us",
"name": "Show F",
"quality": "720p",
"runtime": "180",
"time": "2017-09-10 16:00:00",
"version": "None"
},
],
"name": "02 - CBS"
},
"3": {
"channel_id": "3",
"img": "https://guide.tv/assets/images/channels/3.png",
"items": [
{
"category": "Comedy",
"channel": "3",
"description": "Latest episode of show G",
"end_time": "2017-09-18 12:00:00",
"language": "us",
"name": "Show G",
"quality": "hqlq",
"runtime": "120",
"time": "2017-09-18 10:00:00",
"version": "None"
},
{
"category": "Action",
"channel": "3",
"description": "Latest episode of show H",
"end_time": "2017-09-19 12:00:00",
"language": "us",
"name": "Show H",
"quality": "hqlq",
"runtime": "120",
"time": "2017-09-19 10:00:00",
"version": "None"
},
],
"name": "03 - ABC"
}
}
This is the code I have tried:
import json
with open('file.json') as data_file:
data = json.load(data_file)
for element in data.values():
if 'items' in element:
for e2 in element['items']:
if '2017-09-10' in e2['time']:
del e2
print json.dumps(data, indent=4, sort_keys=True)