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