I have provided an example of what the JSON looks like. I am trying to print each drivers information separately but can only print the key "Drivers".
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/current/drivers.json",
"limit": "30",
"offset": "0",
"total": "21",
"DriverTable": {
"season": "2021",
"Drivers": [
{
"driverId": "alonso",
"permanentNumber": "14",
"code": "ALO",
"url": "http://en.wikipedia.org/wiki/Fernando_Alonso",
"givenName": "Fernando",
"familyName": "Alonso",
"dateOfBirth": "1981-07-29",
"nationality": "Spanish"
},
{
"driverId": "bottas",
"permanentNumber": "77",
"code": "BOT",
"url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
"givenName": "Valtteri",
"familyName": "Bottas",
"dateOfBirth": "1989-08-28",
"nationality": "Finnish"
}
]
}
}
}
Below is a snippet of the piece of code dealing with the json
print("The drivers for the current season are : \n\n")
succeeded = False
api_url = r"http://ergast.com/api/f1/current/drivers.json?"
response = requests.get(api_url)
if response.status_code == 200:
response_json = json.loads(response.content)
#for items in response_json:
for (k,v) in response_json.items():
for (i,j) in v.items():
for data in j:
drivers = ''
drivers = data
#print("Key:",i,"value :", j)
print(drivers)
CodePudding user response:
Assuming this JSON object is named json
, this will print out each driver:
drivers = json["MRData"]["DriverTable"]["Drivers"]
for driver in drivers:
print(driver)