[Solved] Access the key of a multilevel JSON file in python


You can use next with a generator comprehension.

res = next(i['name'] for i in json_dict['ancestors'] if
           i['subcategory'][0]['key'] == 'province')

# 'Lam Dong Province'

To construct the condition i['subcategory'][0]['key'], you need only note:

  1. Lists are denoted by [] and the only element of a list may be retrieved via [0].
  2. Dictionaries are denoted by {} and values may be retrieved via [key].

solved Access the key of a multilevel JSON file in python