skip to Main Content

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


  1. 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.

    import json
    import pandas as pd
    
    with open('case_study.json', encoding='utf-8') as file:
        data = json.load(file)
    
    df = pd.DataFrame(data['data'])
    
    filtered_df = df[df['workplace_type_text'] == 'Fully remote']
    
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    
    print(filtered_df)
    
    
    Login or Signup to reply.
  2. Your data json object is a dictionary, you can see it by:

    type(data)
    # dict
    

    data has only one key named 'data':

    data.keys()
    # dict_keys(['data'])
    

    so you’re going to have to filter data['data'] by values.

    To extract the desired items use filter:

    result = list(filter(lambda x: x['workplace_type_text'] == 'Fully remote', data['data']))
    

    or else a simple list comprehension:

    result = [job for job in data['data'] if job['workplace_type_text'] == 'Fully remote']
    

    Visualize result for instance:

    [(job['title'], job['workplace_type_text']) for job in result] 
    # [('Senior Sales Account Executive ', 'Fully remote'),
    #  ('Senior Software Developer', 'Fully remote'),
    #  ('Customer Success Manager', 'Fully remote'),
    #  ('Senior Sales Account Executive ', 'Fully remote'),
    #  ('Customer Success Manager', 'Fully remote'),
    #  ('Community Marketing Manager', 'Fully remote'),
    #  ('Partnerships Marketing Manager', 'Fully remote'),
    #  ('Head of Support Engineering (Rails)', 'Fully remote'),
    #  ('Head of Support Engineering (Rails)', 'Fully remote'),
    #  ('Demand Generation Manager', 'Fully remote'),
    #  ('Partnerships Marketing Manager', 'Fully remote')]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search