I've loaded json data to api_result
variable. Now I need to extract particular fields (name
, surname
, city
etc.). How should I verify if they are there?
api_result = json.loads(some_json_data)
if api_result.get('name'):
# do something with name
if api_result.get('surname'):
# do something with surname
if api_result.get('city'):
# do something with city
Is it correct approach? Looks too complicated. Is there any way to get empty value if value is not found?
get() in Python has a default parameter, if the value is not found the default value will be retuned.
print dict.get("name", "<default name>")