I’m working with an external API and trying to automatically feed data from it into my own application.
Let’s say that the API call i’m making always has the fields ‘length’ and ‘girth’ and sometimes has the field ‘colour’.
The way I have currently set it up to extract the data from the API call is like this:
python
resp = request.get(some_api_call)
length = resp.json()['length']
girth = resp.json()['girth']
try:
colour = resp.json()['colour']
except:
colour = 'unknown'
# do something with data all three variables.
My question is how should i do this without using try and except? I am sure I am not using it how it was intended and I want to ensure good quality code.