This one is tricky... There are a number of questions and answers on how to traverse trees, but I could not adapt any of the proposed solutions to my special case. My problem is pretty close to Python: How can I filter a n-nested dict of dicts by leaf value?
I have JSON data with a special nested structure (subterms, synonyms, name, id) which can be of arbitrary depth.
tree=[{'id': 20, 'name': 'education', 'subterms': [
{'id': 21, 'name': 'schools', 'synonyms': []},
{'id': 22, 'name': 'schoolbooks', 'synonyms': ['literature']},
{'id': 23, 'name': 'higher education', 'synonyms': ['university']},
{'id': 25, 'name': 'conference', 'synonyms': ['lecture']}]},
{'id': 26, 'name': 'health', 'subterms': [
{'id': 27, 'name': 'health issues', 'synonyms': []},
{'id': 28, 'name': 'nutrition', 'synonyms': []},
{'id': 29, 'name': 'medicine', 'synonyms': []}]},
{'id': 1, 'name': 'business', 'subterms': [{'id': 2,
'name': 'industry',
'subterms': [{'id': 21, 'name': 'service', 'synonyms': []},
{'id': 21, 'name': 'agriculture', 'synonyms': []}],
'synonyms': []},
{'id': 3, 'name': 'professions', 'synonyms': ['jobs']}]}]
My aim is to filter this tree by matches for 'name' and 'synonyms'. The branch hierarchy of a matching term has to be preserved: A matching subterm on level 3 would mean that the parent terms on levels 1 and 2 are also preserved (but not the subterms).
For example the use of filterterms=['literature', 'agriculture']
should result in the following filtered tree:
[{'id': 20, 'name': 'education', 'subterms': [
{'id': 22,'name': 'schoolbooks', 'synonyms': ['literature']}]},
{'id': 1, 'name': 'business', 'subterms': [{'id': 2, 'name': 'industry',
'subterms': [{'id': 21, 'name': 'agriculture', 'synonyms': []}],
'synonyms': []}]}]
All my attempts to traverse the tree on n-levels and preserve the branch hierarchy for the matching terms have so far failed badly... Any help on how I can solve this task?