Skip to content

Instantly share code, notes, and snippets.

@kerenskybr
Last active May 11, 2023 14:38
Show Gist options
  • Save kerenskybr/c7b622e0a418249f46002d93e877dbf8 to your computer and use it in GitHub Desktop.
Save kerenskybr/c7b622e0a418249f46002d93e877dbf8 to your computer and use it in GitHub Desktop.
Remove null keys and values from a nested structure of dicts, tuple and lists
def remove_none(json_file):
""" Remove null keys and values from
a nested structure of dicts, tuple and lists.
"""
if isinstance(json_file, (list, tuple, set)):
return type(json_file)(remove_none(x) for x in json_file if x is not None)
elif isinstance(json_file, dict):
return type(json_file)((remove_none(k), remove_none(v))
for k, v in json_file.items() if k is not None and v is not None)
else:
return json_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment