title: How to filter nested JSON and preserve branch hierarchy for matches?
body: <p>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 <a href="https://stackoverflow.com/questions/11165381/python-how-can-i-filter-a-n-nested-dict-of-dicts-by-leaf-value">Python: How can I filter a n-nested dict of dicts by leaf value?</a></p>
  
  <p>I have JSON data with a special nested structure (subterms, synonyms, name, id) which can be of arbitrary depth.</p>
  
  <pre><code>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']}]}]
  </code></pre>
  
  <p>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).</p>
  
  <p>For example the use of <code>filterterms=['literature', 'agriculture']</code> should result in the following filtered tree:</p>
  
  <pre><code>[{'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': []}]}]
  </code></pre>
  
  <p>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?</p>