1

I have object like:

et = {  
    "applications": [
        {
        "applications_application": 'value',
        "application_journalNumber": 'value',
        "appParticipants": [
            {
                "appParticipant_inn": "value",
                "appParticipant_kpp": "value",
                "legalForm_code": "value"
            }
            ]
        }
    ]
}

I need to iterate recursively through all keys of it. How I can do it? I would like to get something like:

applications
applications_application
application_journalNumber
appParticipants
appParticipant_inn
appParticipant_kpp
legalForm_code

My not working solution:

def myprint(d):
    for k, v in d.items():
      if isinstance(v, dict):
        myprint(v)
      else:
        if isinstance(v, list):
            myprint(v[0])

P.S. every array have only one item in it.

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • Possible duplicate of [How to completely traverse a complex dictionary of unknown depth?](https://stackoverflow.com/questions/12507206/how-to-completely-traverse-a-complex-dictionary-of-unknown-depth) – Holloway Sep 02 '19 at 10:43
  • This is json object, not Python. You have transform it to Dictionary first using methods from json module. – Yuri Ginsburg Sep 02 '19 at 10:51
  • What is not working? Do you have a result or stacktrace? You current implementation has no end condition, which I guess should be a `print()` in your case. – aveuiller Sep 02 '19 at 10:59

3 Answers3

2

How about:

def traverse_dict(d):
    keys = []
    for key, item in d.items():
        keys.append(key)
        if isinstance(item, dict):
            keys.extend(traverse_dict(item))
        elif isinstance(item, list):
            for d in item:
                keys.extend(traverse_dict(d))
    return keys
jhansen
  • 1,096
  • 1
  • 8
  • 17
0

You're not far off a working solution, I can propose you this one:

def myprint(d):
    # print("Handling %s" % d)  # Debug logging

    # This is the end condition on which you actually print the entry
    if not isinstance(d, dict):
        print("Entry: %s" % d)
    else:
        for k, v in d.items():
            # print("Handling key: %s - value: %s" % (k, v))  # Debug logging
            if isinstance(v, list):
                myprint(v[0])
            else:
                myprint(k) # replace by myprint(v) to show the values 

Which will print all values found in the dictionary inline.

The output will be as follows:

Entry: applications_application
Entry: application_journalNumber
Entry: appParticipant_inn
Entry: appParticipant_kpp
Entry: legalForm_code

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aveuiller
  • 1,511
  • 1
  • 10
  • 25
0

You can use recursion with a generator:

def _keys(d):
  for a, b in d.items():
    yield a
    if isinstance(b, dict):
      yield from _keys(b)
    if isinstance(b, list):
      for i in b:
         yield from _keys(i)


et = {'applications': [{'applications_application': 'value', 'application_journalNumber': 'value', 'appParticipants': [{'appParticipant_inn': 'value', 'appParticipant_kpp': 'value', 'legalForm_code': 'value'}]}]}
print('\n'.join(_keys(et)))

Output:

applications
applications_application
application_journalNumber
appParticipants
appParticipant_inn
appParticipant_kpp
legalForm_code
Ajax1234
  • 69,937
  • 8
  • 61
  • 102