JSearch

FREEMIUM
Verified
By OpenWeb Ninja | Updated 4 ore fa | Data
Popularity

9.9 / 10

Latency

1,743ms

Service Level

100%

Health Check

100%

Back to All Discussions

Key Error trying to get to "data" with Python

Rapid account: Sara M 2261
SaraM2261
15 days ago

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!

Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented 14 days ago

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.

Rapid account: Sara M 2261
SaraM2261 Commented 14 days ago

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)

    jobsData = response.text
    parsedJobsData = json.loads(jobsData) #this was the winner :)
    readyData = parsedJobsData['data'] #then this worked
Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented 15 days ago

What does print(responseData.keys()) prints out?

Rapid account: Sara M 2261
SaraM2261 Commented 15 days ago

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

Rapid account: Sara M 2261
SaraM2261 Commented 15 days ago

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.

Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented 15 days ago

What is the value of responseData after doing responseData = response.json()?

Also, what does print(type(responseData)) outputs?

Rapid account: Sara M 2261
SaraM2261 Commented 15 days ago

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

Rapid account: Sara M 2261
SaraM2261 Commented 15 days ago

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.

Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented 15 days ago

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

Join in the discussion - add comment below:

Login / Signup to post new comments