Hello!
I can retrieve the job listings, and I can take a user input for the query string to filter just the jobs with that keyword. However, trying to use the data key to display items on the front side, I am getting a key error every time I try to get to the data key (where all the jobs are stored as items). (I am using Python)
querystring = {“query”:{jobSearchInput},“page”:“1”,“num_pages”:“1”,“date_posted”:“week”,“employment_types”:“FULLTIME”}
response = requests.get(url, headers=headers, params=querystring)
responseData = response.json()
appData = responseData[‘data’]
^ the last line should work but throws a key error saying the data key does not exist. I am printing the parsed response prior to this and I can see the data key in it.
The data is there - on the front-end page I am printing it whole successfully without being able to access the “data” key.
Thank you for any help!
Junte-se à discussão - adicione o comentário abaixo:
Nice, great to hear the issue is resolved!
Please don’t hesitate to reach out in case you have any questions or if anything is needed.
Good morning!
I was able to get it working with a different method. Here’s what I used instead of response.json()
response = requests.get(url, headers=headers, params=querystring)
What does print(responseData.keys()) prints out?
the value of responseData is that parsed JSON object. Here’s the start of it after a user search:
{‘status’: ‘OK’, ‘request_id’: ‘545299bb-7101-4421-b9af-cc980f84cdc2’, ‘parameters’: {‘query’: ‘banana’, ‘page’: 1, ‘num_pages’: 1, ‘date_posted’: ‘week’, ‘employment_types’: [‘FULLTIME’]}, ‘data’: [{‘job_id’: ‘BJFHKQKF3QSc4P48AAAAAA==’, ‘employer_name’: ‘Banana Republic’, ‘employer_logo’: ‘https://bananarepublic.gap.com/Asset_Archive/BRWeb/Assets/Marketing/BR_Logo_R1.jpg’, ‘employer_website’: ‘http://www.gapinc.com’, ‘employer_company_type’: ‘Retail’, ‘job_publisher’: ‘Gap Inc.’, ‘job_employment_type’: ‘FULLTIME’, ‘job_title’: ‘Lead, Full Time - Somerset Collection’, ‘job_apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘job_apply_is_direct’: False, ‘job_apply_quality_score’: 0.6787, ‘apply_options’: [{‘publisher’: ‘Gap Inc.’, ‘apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘is_direct’: False}, {‘publisher’: ‘LinkedIn’, ‘apply_link’:
responseData is a variable that holds that parsed JSON response from the API.
print(type(responseData)) is checking the type to make sure it was the correct type of object and didn’t need further parsing before using the
appData = responseData[‘data’] to get to the data key and the items inside it for displaying.)
The output is <class ‘dict’> stating that it is a Python dictionary and so that responseData[‘data’] should work to access the data key items.
What is the value of responseData after doing responseData = response.json()?
Also, what does print(type(responseData)) outputs?
Here is the start of the JSON object that I am trying to get to the key 'data’
As the user I entered “banana” for the search query - you can see it is a string below. And also that ‘data’ key is present.
Live Results
{‘status’: ‘OK’, ‘request_id’: ‘545299bb-7101-4421-b9af-cc980f84cdc2’, ‘parameters’: {‘query’: ‘banana’, ‘page’: 1, ‘num_pages’: 1, ‘date_posted’: ‘week’, ‘employment_types’: [‘FULLTIME’]}, ‘data’: [{‘job_id’: ‘BJFHKQKF3QSc4P48AAAAAA==’, ‘employer_name’: ‘Banana Republic’, ‘employer_logo’: ‘https://bananarepublic.gap.com/Asset_Archive/BRWeb/Assets/Marketing/BR_Logo_R1.jpg’, ‘employer_website’: ‘http://www.gapinc.com’, ‘employer_company_type’: ‘Retail’, ‘job_publisher’: ‘Gap Inc.’, ‘job_employment_type’: ‘FULLTIME’, ‘job_title’: ‘Lead, Full Time - Somerset Collection’, ‘job_apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘job_apply_is_direct’: False, ‘job_apply_quality_score’: 0.6787, ‘apply_options’: [{‘publisher’: ‘Gap Inc.’, ‘apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘is_direct’: False}, {‘publisher’: ‘LinkedIn’, ‘apply_link’:
That is actually a variable that takes a user input. I can send an image of the object that is returned that I am working with. That variable is a string in the object after it runs.
Hi there,
Thanks for your message.
Try to replace “{jobSearchInput}” with “jobSearchInput” (in querystring query), resulting in: querystring = {“query”:jobSearchInput,“page”:“1”,“num_pages”:“1”,“date_posted”:“week”,“employment_types”:“FULLTIME”}
It seems like a set is passed to the “query” instead of a string.
Does it work for you?
Thank you,
Adam @ OpenWeb Ninja