In this answer I understand that I can use the GET instruction to catch an element of a JSON file and get a None
if this does not exists. But I can't understand how to use with more than one level of JSON data.
Let's have a look at the following example:
import json
json_input = '{"persons": [{"name": "Brian", "city": "Seattle"}, {"name": "David", "city": "Amsterdam"} ] }'
d = json.loads(json_input)
- Input
d.get('persons')
output
[{'name': 'Brian', 'city': 'Seattle'}, {'name': 'David', 'city':'Amsterdam'}]
- Input
d.get('animals')
output
None
but I would like to use the same to check if d['persons'][0]['name']
exist, or if d['persons'][0]['tel_number']
.
If I use d.get(['persons'][0]['name'])
I get the following error:
Traceback (most recent call last): File "", line 1, in d.get(['persons'][0]['name']) TypeError: string indices must be integers