Parsing a returned JSON in Python and checking if values exist

I am grabbing some JSON data from an online site and have the below:-

try:
    data = response.read()  
    json_response = json.loads(source)
    name = json_response['profiles'][0]['content']['nameFull']
    first_name = json_response['profiles'][0]['content']['name']['first']
    surname = json_response['profiles'][0]['content']['name']['last']
    employment_type = json_response['profiles'][0]['content']['employeeType']['title']
except:
    continue

For each of the variables assigned in the above, I then perform an action with each variable. This works fine IF all of the values in the JSON exist but, if for example the ‘title’ entry isn’t there, then this fails. How can I handle this without looking to add a ‘try/except’ on each variable? Is there a more Pythonesque way of handling this? Likewise, is there a way to add a default value if it doesn’t exist at the top layer as opposed to per JSON entry level?

Thanks

>Solution :

Not sure if that helps, but here’s what I found:

  1. Use the get() method: The get() method allows you to specify a default value that will be returned if the key you’re trying to access does not exist in the JSON. This can be a more elegant solution than using multiple try-except blocks, as you can specify the default value for each key in a single line of code. Example:

name = json_response.get('profiles')[0].get('content').get('nameFull', 'N/A')

  1. Use the dict.setdefault() method: The setdefault() method allows you to set a default value for a key if it doesn’t exist. This method will add the key-value pair to the dictionary only if the key doesn’t exist. Example:

    json_response['profiles'][0]['content'].setdefault('employeeType', {}).setdefault('title', 'N/A')

  2. Use recursion: Use recursion to traverse the json data, check for the existence of each key before accessing it. This can be useful if you need to handle missing data at multiple levels in the JSON.

def get_json_value(json_data, keys, default=None):
    if keys and json_data:
        key = keys.pop(0)
        if key in json_data:
            return get_json_value(json_data[key], keys, default)
    return json_data or default

name = get_json_value(json_response, ['profiles', 0, 'content', 'nameFull'], 'N/A')

4.Use json_normalize from pandas library to flatten the json and use the fillna method to set a default value for missing fields.

import pandas as pd

json_df = pd.json_normalize(json_response)
json_df.fillna('N/A', inplace=True)

Leave a Reply