Skip to content

Instantly share code, notes, and snippets.

@noize-e
Last active March 31, 2021 20:45
Show Gist options
  • Save noize-e/c90082535a86c2485b119e9efc6c9df4 to your computer and use it in GitHub Desktop.
Save noize-e/c90082535a86c2485b119e9efc6c9df4 to your computer and use it in GitHub Desktop.
Digest a JSON file
import json
import sys
"""JSON Digest
Python script to digest a JSON structure.
Tasks:
1. Get each key name and corresponding value
a. Get the current value.
b. Get the value's data type.
2. Get the unique keys names.
a. Ignore one or more keys.
b. Set tree level digest limit
"""
# Settings
ignore_keys = ['email mappings']
last_level = 0
# Flags
first_round = True
type_instead = True
full_output = True
exit_on_first_round = False
# Data holders
level_keys = {}
def get_tabs(level):
return "".join([" " for x in range(0, level - 1)])
def digest_dictionary(src, level):
for key in src.keys():
digest_data(src[key], level, key)
def add_key_to_list(key, level):
global level_keys
try:
if key not in level_keys[level]:
level_keys[level].append(key)
print(get_tabs(level) + key)
except Exception:
# Create a list per level if there isn't one
level_keys[level] = [key]
print(get_tabs(level) + key)
def digest_data(src, level=0, key=""):
# Settings
global last_level
global ignore_keys
# Flags
global first_round
global type_instead
global full_output
if key not in ignore_keys:
# Ig level one is reached and
# the first_round flag is off
# exit
if level == 5 and not first_round and exit_on_first_round:
return
else:
level += 1
# when the deepest level is reached
# from the first round turn off the
# first_round flag
if (last_level + 1) != level:
first_round = False
if type(src) is dict:
# Before going deeper keep save
# temporaly the current_level
last_level = level
# tab = get_tabs(level)
output = "{}: # [M] Level:{}".format(key, level)
# print(tab + output)
add_key_to_list(key, level)
digest_dictionary(src, level)
elif type(src) is list:
last_level = level
for item in src:
digest_data(item, level, key)
else:
if full_output:
output = "{}: {} # {} [A]"
# TODO: Add show_level flag
outlevel = "[L:{}]".format(level)
outtype = "{}".format(type(src))
# On type_instead set as value the data type
outvalue = src if not type_instead else outtype
# Soround any string with single quotes
if type(src) is str:
outvalue = "'{}'".format(outvalue)
print(get_tabs(level) + output.format(
key, outvalue, outlevel))
else:
# print(get_tabs(level) + key)
add_key_to_list(key, level)
def main():
try:
with open(sys.argv[1]) as file:
digest_data(json.loads(file.read()))
except Exception:
print("Usage: python json_digest.py <filename>.json")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment