Last active
November 16, 2018 10:12
-
-
Save codeSamuraii/9a3d9c6c916bc68cac24f0a58de2c5d6 to your computer and use it in GitHub Desktop.
Parse Zoho Recruit JSON response into a Python dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
# Input: Zoho JSON response as a string - Output : {'First Name': 'John', 'Last Name': 'Doe', 'Phone': ... } | |
# To be used like a generator (i.e `for candidate in parse_json_iter(zoho_response):`) | |
# `get_fields` allows you to specify fields to get, the others will be dismissed. | |
# `module` = 'Candidates', 'JobOpenings'... (haven't tried with others) | |
def parse_json_iter(raw_json_response: str, module: str = 'Candidates', get_fields: list = []): | |
"""Parse through a JSON response of candidates.""" | |
# Load the string into a dictionnary | |
json_response = json.loads(raw_json_response)['response']['result'][module]['row'] | |
# If there is a single item, put it in a list with one element | |
if json_response and type(json_response) == dict: | |
json_response = [json_response] | |
# Iterate through the rows | |
for entry in json_response: | |
entry_df = pd.DataFrame.from_records(entry['FL']).set_index('val') | |
entry_dict = entry_df.to_dict()['content'] | |
if not get_fields: | |
yield entry_dict | |
else: | |
if len(get_fields) == 1: | |
yield entry_dict[get_fields[0]] | |
else: | |
yield {field: value for field, value in entry_dict.items() if field in get_fields} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found that Zoho's structured data responses (XML & JSON) were particularly badly designed, this method could save you a lot of struggle.