I am writing a script to create a csv based on a parsed JSON. I am able to read the JSON, but get hung up with a TypeError: list indices must be integers, not dict.
I have a previous version that is working, but with a different JSON, so the structure is slightly different, and I am working on my reverse engineering skills, but am stumped on extracting with the slight differences.
JSON is structured thus:
{
"league": {
"games": {
"0": [
{
"game": {
"game_number": "game_1",
"season": "2019",
"start_time": "Sat, 13 Apr 2019 23:00:00",
"team_id": [
{
"away_team": "team_x"
},
{
"home_team": "team_a"
},
],
},
},
],
},
},
}
data = json_parsed['league']['games'][0]
with open('./soccer_041519.csv', 'w+') as csvFile:
for game in data:
gameid = data[game]['game_number']
start_time = data[game]['start_time']
home_team_id = data[game]['home_team']
away_team_id = data[game]['away_team']
csvFile.write("%s @ %s,%s, ,%s\n"%(away_team_id, home_team_id, gameid, start_time))
The values should be written to the CSV
"game_number": "game_1"needs a trailing comma, as do the next two items. Also, the dictionaries inteam_idneed to be separated with commas.