Last active
May 11, 2023 14:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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