Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add key to the nested dictionary recursively based on condition

I have a multi level nested dictionary, trying to add a key required whenever there is type key in that dictionary.

Code:

def add_recursively(val):
   if 'type' in val and val['type'] != 'object':
      val['required'] = False
      return val
   elif 'type' in val and val['type'] == 'object' and 'object_schema' in val:
      return add_recursively(val['object_schema'])
   else:
     val['required'] = False
     return val

new_data = {}
for key, value in data.items():
    if value['type'] == 'object' and 'object_schema' in value:
       new_data[key] = add_recursively(value)
    else:
       value['required'] = False
       new_data[key] = value

Input data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data = {'key1': {'description': 'Configuration for OpenID', 'type': 'boolean'},
 'key2': {'default': {'enabled': True, 'logging_percent': 100},
  'object_schema': {'enabled': {'default': True, 'type': 'boolean'},
   'logging_percent': {'default': 100, 'type': 'float'},
   'nested_key': {'default': {'new_enable': True, 'new_precent': 100},
    'object_schema': {'new_enable': {'default': True, 'type': 'boolean'},
     'new_precent': {'default': 100, 'type': 'float'}},
    'type': 'object'}},
  'type': 'object'}}

Output data:

{'key1': {'description': 'Configuration for OpenID',
  'required': False,
  'type': 'boolean'},
 'key2': {'enabled': {'default': True, 'type': 'boolean'},
  'logging_percent': {'default': 100, 'type': 'float'},
  'nested_key': {'default': {'new_enable': True, 'new_precent': 100},
   'object_schema': {'new_enable': {'default': True, 'type': 'boolean'},
    'new_precent': {'default': 100, 'type': 'float'}},
   'type': 'object'},
  'required': False}}

Expected output:

{'key1': {
  'required': False,
  'description': 'Configuration for OpenID',
  'type': 'boolean'},
 'key2': {
     'required': False,
     'enabled': {'required': False, 'default': True, 'type': 'boolean'},
    'logging_percent': {'required': False, 'default': 100, 'type': 'float'},
    'nested_key': {'default': {'new_enable': True, 'new_precent': 100},
    'object_schema': {'new_enable': {'required': False, 'default': True, 'type': 'boolean'},
      'new_precent': {'required': False, 'default': 100, 'type': 'float'}},
    'type': 'object'},
    }}

>Solution :

Here’s one way that modifies data in-place by adding required key to each key that has type key in it:

def add_required(d):
    if 'type' in d:
        d['required'] = False
    for v in d.values():
        if isinstance(v, dict):
            add_required(v)
add_required(data)

Output:

>>> data
{'key1': {'description': 'Configuration for OpenID',
  'type': 'boolean',
  'required': False},
 'key2': {'default': {'enabled': True, 'logging_percent': 100},
  'object_schema': {'enabled': {'default': True,
    'type': 'boolean',
    'required': False},
   'logging_percent': {'default': 100, 'type': 'float', 'required': False},
   'nested_key': {'default': {'new_enable': True, 'new_precent': 100},
    'object_schema': {'new_enable': {'default': True,
      'type': 'boolean',
      'required': False},
     'new_precent': {'default': 100, 'type': 'float', 'required': False}},
    'type': 'object',
    'required': False}},
  'type': 'object',
  'required': False}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading