Created
February 15, 2022 05:25
-
-
Save kishorek/287567df548df6961dcd4c0d754e793b to your computer and use it in GitHub Desktop.
Print all the keys in JSON
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
import json | |
""" | |
This script will print all the JSON keys present in a JSON | |
""" | |
FILE = "<file_path here>" | |
DIVIDER = "." | |
ARRAY_INDICATOR = "[]" | |
def print_keys(json_data, prefix=""): | |
for key, val in json_data.items(): | |
if isinstance(val, dict): | |
print_keys(val, prefix+DIVIDER+key if prefix else key) | |
elif isinstance(val, list): | |
if len(val) > 0 and isinstance(val[0], dict): | |
print_keys(val[0], prefix+DIVIDER+key+ARRAY_INDICATOR) | |
else: | |
# print(prefix+DIVIDER+key+","+str(type(val)) if prefix else key+","+str(type(val))) | |
print(prefix+DIVIDER+key if prefix else key) | |
with open(FILE, "r") as f: | |
json_data = json.load(f) | |
print_keys(json_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment