Python - Getting a list in a nested JSON

Viewed 52

I'm relatively new to programming, sorry I've the question was answerd already.

I'm trying to get a specific value from a JSON I've got from an API. The JSON looks like this after I tabbed into the first {} and saved it into a list.

Now I need to extract all "term" values. Tried many approaches but I couldn't find the right approach for it.

[
  [
    {
      'id': 292,
      'categories': [
        
      ],
      'terms': [
        {
          'term': 'Erprobung'
        },
        {
          'term': 'Probe'
        },
        {
          'term': 'Prüfung'
        },
        {
          'term': 'Test'
        },
        {
          'term': 'Versuch'
        }
      ]
    },
    {
      'id': 4398,
      'categories': [
        
      ],
      'terms': [
        {
          'term': 'Leistungsnachweis'
        },
        {
          'term': 'Prüfung'
        },
        {
          'term': 'Test'
        }
      ]
    },
    {
      'id': 5752,
      'categories': [
        
      ],
      'terms': [
        {
          'term': 'Klassenarbeit'
        },
        {
          'term': 'Klausur'
        },
        {
          'term': 'Leistungsüberprüfung'
        },
        {
          'term': 'Lernerfolgskontrolle'
        },
        {
          'term': 'Prüfung'
        },
        {
          'term': 'Schularbeit'
        },
        {
          'term': 'Schulaufgabe'
        },
        {
          'term': 'Test'
        },
        {
          'term': 'Arbeit',
          'level': 'umgangssprachlich'
        }
      ]
    },
    {
      'id': 9138,
      'categories': [
        
      ],
      'terms': [
        {
          'term': 'Experiment'
        },
        {
          'term': '(die) Probe aufs Exempel'
        },
        {
          'term': 'Probelauf'
        },
        {
          'term': 'Studie'
        },
        {
          'term': 'Test'
        },
        {
          'term': 'Testballon'
        },
        {
          'term': 'Testlauf'
        },
        {
          'term': 'Trockenlauf'
        },
        {
          'term': 'Trockentest'
        },
        {
          'term': 'Versuch'
        },
        {
          'term': 'Versuchsballon'
        }
      ]
    },
    {
      'id': 6241,
      'categories': [
        
      ],
      'terms': [
        {
          'term': 'Bewährungsprobe'
        },
        {
          'term': 'Feuerprobe'
        },
        {
          'term': 'Feuertaufe'
        },
        {
          'term': 'harte Prüfung'
        },
        {
          'term': 'Lackmustest'
        },
        {
          'term': 'Nagelprobe'
        },
        {
          'term': 'Test'
        }
      ]
    }
  ]
]
2 Answers

The simplest way is to use a nested list comprehension:

>>> [term_dict["term"] for d in my_list for term_dict in d["terms"]]

['Erprobung', 'Probe', 'Prüfung', 'Test', 'Versuch', 'Leistungsnachweis',
'Prüfung', 'Test', 'Klassenarbeit', 'Klausur', 'Leistungsüberprüfung',
'Lernerfolgskontrolle', 'Prüfung', 'Schularbeit', 'Schulaufgabe', 'Test',
'Arbeit', 'Experiment', '(die) Probe aufs Exempel', 'Probelauf', 'Studie',
'Test', 'Testballon', 'Testlauf', 'Trockenlauf', 'Trockentest', 'Versuch',
'Versuchsballon', 'Bewährungsprobe', 'Feuerprobe', 'Feuertaufe', 'harte Prüfung',
'Lackmustest', 'Nagelprobe', 'Test']

Well, after starting from scrach I've resolved my problem.

new_list = []
for list_num in synonym_call:
    # ic(len(list_num))
    # ic(list_num[ITERATE TROUGH HERE]["terms"])
    # ic(list_num[len(list_num) - 1]["terms"])

    list_length = len(list_num) - 1
    while list_length > 0:
        new_list.extend(list_num[list_length - 1]["terms"])
        list_length -= 1

synonym_list = []
for term in new_list:
    synonym_list.append(term["term"])
    
print(synonym_list)

And that's the outcome:

['Experiment', '(die) Probe aufs Exempel', 'Probelauf', 'Studie', 'Test', 'Testballon', 'Testlauf', 'Trockenlauf', 'Trockentest', 'Versuch', 'Versuchsballon', 'Klassenarbeit', 'Klausur', 'Leistungsüberprüfung', 'Lernerfolgskontrolle', 'Prüfung', 'Schularbeit', 'Schulaufgabe', 'Test', 'Arbeit', 'Leistungsnachweis', 'Prüfung', 'Test', 'Erprobung', 'Probe', 'Prüfung', 'Test', 'Versuch']

I'm pretty sure that there is an even easier approach to it but for now it works and im more than happy with it! Thank you all for your help!

Related