Reputation: 71
I want to solve the following problem. I have variable data_json which contain json data of following structure
data_json =
"api": {"results": 402,
"fixtures":
[{"fixture_id": 127807,
"league_id": 297,
"round": "Clausura - Semi-finals",
"statusShort": "FT",
"elapsed": 90,
"venue": "Estadio Universitario de N)",
"referee": null,
"homeTeam": {"team_id": 2279,
"team_name": "Tigres UANL",
"logo": "url"},
"awayTeam": {"team_id": 2282,
"team_name": "Monterrey",
"logo": "url1"},
"goalsHomeTeam": 1,
"goalsAwayTeam": 0,
"score": {"halftime": "1-0",
"fulltime": "1-0",
"extratime": null,
"penalty": null}},
....... another fixture-ids
}
}
the same piece of data in another fixture_id
I need to extract values of each key:value pairs and save extracted values to variables. I need the following output
var fixture_id = fixture_id[value]
var league_id = league_id[value]
var round = round[value]
var statusshort = statusShort[value]
var elapsed = elapsed[value]
var venue = venue[value]
var referee = referee[value]
var home_team_id = homeTeam.team_id[value]
var home_team_name = homeTeam.team_name[value]
var home_team_logo = homeTeam.logo[value]
##the same for awayTeam dictionary
I tried looping on my json by following code
data_json_looping_on = data_json["api"]["fixtures"]
for id in data_json_looping_on:
fixture_id = id["fixture_id"]
league_id = id["league_id"]
the same logic for each
simple key value pairs. But when
looping go up on dictionariy i tried
following loop
for item in id["homeTeam"]
home_team_id = item["team_id"]
Here is Traceback of above code
------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-958a38c41236> in
<module> 18 venue = item["venue"]
19 referee = item["referee"] ---> 20
for item in id["homeTeam"]: 21
home_team_id = item["team_id"] 22
print(home_team_id) TypeError:
string indices must be integers
------------------------------------------------------
I tried another method
data_json_looping_on = data_json["api"]["fixtures"]
for id in data_json_looping_on:
fixture_id = id["fixture_id"]
league_id = id["league_id"]
#the same for logic for each
simple key value pairs.
But when go up on
home_team_id = id["homeTeam"]["team_id"]
print(home_team_id)
Python arrise the following error
------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-1fe42572d5ae> in
<module> 18 venue = item["venue"]
19 referee = item["referee"] ---> 20
home_team_id = id["homeTeam"]
["team_id"] 21 print(home_team_id)
TypeError: string indices must be integers
How i can extract values of homeTeam and awayTeam dictionaries? If you have another logic of extracting give me your advise
Here is my real code
date_fixt = open("../forecast/endpoints/date_fixtures.txt", "r")
date_fixt_json = json.load(date_fixt)
data_json = date_fixt_json["api"]["fixtures"]
for item in data_json:
fixture_id = item["fixture_id"]
league_id = item["league_id"]
event_date = item["event_date"]
event_timestamp = item["event_timestamp"]
firstHalfStart = item["firstHalfStart"]
secondHalfStart = item["secondHalfStart"]
round_count = item["round"]
status = item["status"]
statusShort = item["statusShort"]
elapsed = item["elapsed"]
venue = item["venue"]
referee = item["referee"]
for item in id["homeTeam"]:
home_team_id = item["team_id"]
print(home_team_id)
Upvotes: 0
Views: 78
Reputation: 782130
id["homeTeam"]
and id["awayTeam"]
aren't lists (there are no square brackets around them), they're just single dictionaries. So you don't need to loop over them.
date_fixt = open("../forecast/endpoints/date_fixtures.txt", "r")
date_fixt_json = json.load(date_fixt)
data_json = date_fixt_json["api"]["fixtures"]
for item in data_json:
fixture_id = item["fixture_id"]
league_id = item["league_id"]
event_date = item["event_date"]
event_timestamp = item["event_timestamp"]
firstHalfStart = item["firstHalfStart"]
secondHalfStart = item["secondHalfStart"]
round_count = item["round"]
status = item["status"]
statusShort = item["statusShort"]
elapsed = item["elapsed"]
venue = item["venue"]
referee = item["referee"]
home_team_id = item["homeTeam"]["team_id"]
print(home_team_id)
away_team_id = item["awayTeam"]["team_id"]
print(away_team_id)
Upvotes: 2
Reputation: 2759
Your json may be formatted improperly if it is thinking that "homeTeam" is reference to a string and not a dictionary.
try:
import json
data_json = json.loads(data_json)
home_team_id = id["homeTeam"]["team_id"]
away_team_id = id["awayTeam"]["team_id"]
The second issue may be that it's failing at:
fixture_id = id["fixture_id"]
because "fixtures" references a list. So you should also try:
fixture_id = data_json_looping_on[0]["fixture_id"]
Upvotes: 1