So i have a case study for an internship and it has the question
Retrieve JSON Data and display data following source : https://workwithus.pinpointhq.com/jobs.json
with the filter: workplace_type_text = Fully remote
I understand that workplace_type_text is the key and i should use it for the filtering. But the JSON file is not like the usual files that are structured in a way that it’s easy to retrieve the data from.
What should i do?
import json
with open('case_study.json', encoding='utf-8') as file:
data = json.load(file)
print(data) # Check the structure and content of the loaded JSON data
filtered_data = [item for item in data if item['workplace_type_text'] == 'Fully remote']
for item in filtered_data:
print(item['name'], item['price'])
I’ve tried that code but that only prints the entirety of the file and shows a TypeError: string indices must be integers, not ‘str’ for the filtered_data = [item for item in data if item['workplace_type_text'] == 'Fully remote']
So i changed it to
import json
with open('case_study.json', encoding='utf-8') as file:
data = json.load(file)
filtered_data = [item for item in data if 'workplace_type_text' in item and item['workplace_type_text'] == 'Fully remote']
print(filtered_data) # Check the contents of the filtered_data list
for item in filtered_data:
print(item['name'], item['price'])
now it only prints out [] and nothing else
trying to check if the key is correct with
import json
with open('case_study.json', encoding='utf-8') as file:
data = json.load(file)
print(data.keys())
also doesn’t print anything. I’m lost did i skip a step? Thank you.
2
Answers
To retrieve info from your "case_study.json" you can easily exploit pandas to create a DataFrame from the ‘data’ object in your JSON file.
Then you can filter data based on the ‘workplace_type_text’ column, if you desire.
Your
data
json object is a dictionary, you can see it by:data
has only one key named'data'
:so you’re going to have to filter
data['data']
by values.To extract the desired items use
filter
:or else a simple list comprehension:
Visualize result for instance: