Created
March 12, 2018 21:45
-
-
Save yinleon/694fa2b2bc1b68975b3b1f8fc429fc1e to your computer and use it in GitHub Desktop.
A function to traverse and list all keys in a JSON or Dictionary
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 get_all_keys(d, key=[]): | |
''' | |
A recursive function that traverses json keys in a dict `d`, | |
and prints the path to all keys | |
''' | |
if not isinstance(d, dict): | |
print(''.join(['["' + k + '"]' for k in key])) | |
return | |
for k, v in d.items(): | |
key_path = key + [k] | |
get_all_columns(d[k], key_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment