0

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 :)

1
  • 2
    json_data_three["employments"][0]["employmentName"] Commented Jul 28, 2021 at 14:22

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I understand, all working now many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.