Suppose I have a json like str and it has mixture of key and dict. I want to set a default value if target key is not existed. I can use get method or try and catch block but it does not look nice. Any better solution? Thanks for your time.
import json
j_str = '''{
"key1":[
{
"val1":1,
"key2":2
}
]
}'''
j_dict = json.loads(j_str)
print(j_dict['key1'][0]['val1'])