Reputation: 67
I want to access 'employmentName' and 'jobTile' values. But I keep on getting this error - KeyError: 'jobTitle' and KeyError: 'employmentName'
Here is my code below, please note I am not sharing the api URL on this question but it is present and working in the code.
api_three_url = "https://xxxx"
json_data_three = requests.get(api_three_url, headers=my_headers).json()
#print(json_data_three) #this prints the whole json data (working!)
print("So you have previously worked in", json_data_three['employmentName'],"as a", json_data_three['jobTitle'])
This is what the JSON looks like in python dict:
{
'hasAddedValue': False,
'employments': [{
'id': 527,
'employmentName': 'Sallys hair',
'jobTitle': 'Stylists',
'jobStartDate': '2019-03',
'jobEndDate': '2020-04',
'jobType': 'PAID',
'status': True,
'isJobCurrent': False
}]
}
Please guide me to where I am going wrong.
Many thanks :)
Upvotes: 0
Views: 113
Reputation: 326
The employmentName
and jobType
fields are not at the top level in your JSON, hence Python is giving you KeyError
. They are actually in one of the objects inside employments
.
So if you want to obtain those fields from the first object inside employments
, it would be:
json_data_three['employments'][0]['employmentName']
Upvotes: 1