I have a json file that looks something like the following:
[
  {
     "category1":"0120391123123"
  },
  [
     {
        "subcategory":"0120391123123"
     },
     [
        {
           "subsubcategory":"019301948109"
        },
        [
           {
              "subsubsubcategory":"013904123908"
           },
           [
              {
                 "subsubsubsubcategory":"019341823908"
              }
           ]
        ]
     ]
  ],
  [
     {
        "subcategory2":"0934810923801"
     },
     [
        {
           "subsubcategory2":"09341829308123"
        }
     ]
  ],
  [
     {
        "category2":"1309183912309"
     },
     [
        {
           "subcategory":"10293182094"
        }
     ]
  ]
]
I also have a list of categories that I would like to find in the original list. If the category exists in categoriesToFind, I would also like to find all subcategories and return those as well.
categoriesToFind = ['019301948109', '1309183912309']
finalCategories = []
def findCategories(currentList, isFirstIteration):
    for x in currentList:
        if type(x) is dict and (next(iter(x.values())) in categoriesToFind or not isFirstIteration):
            finalCategories.append(next(iter(x.values())))
            if len(currentList) < currentList.index(x) + 1:
                findCategories(currentList[currentList.index(x) + 1], False)
findCategories(data, True)
I would want finalCategories to contain the following:
['019301948109', '013904123908', '019341823908', '1309183912309', '10293182094']
 
    