Compare commits

..

2 Commits

Author SHA1 Message Date
d62e875b99 made flatten a runable script 2023-06-13 12:14:48 -04:00
e6166ca827 added flatten utility 2023-06-13 11:29:40 -04:00

87
python/flatten Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env python3
import argparse
import json
import logging
_level = logging.INFO
_format = "%(asctime)-15s [%(levelname)-8s] %(lineno)d : %(funcName)s : %(message)s"
logging.basicConfig(format=_format, level=_level)
logger = logging.getLogger()
def parse_args():
argp = argparse.ArgumentParser()
argp.add_argument(
'--debug',
action='store_true',
help="Run in debug mode"
)
argp.add_argument(
'json_filename',
help="JSON filename to flatten"
)
argp.add_argument(
'--output-file', '-o',
help="Output filename for results"
)
return argp.parse_args()
def flatten(data_object, key_name=None, variable_list=[]):
"""Will flatten a JSON file by listing the variables in
a dot notation.
Args:
data_object: Data as dict, list, string, etc. to parse recursively
key_name: String of the key name to pass into in the next call
variable_list: List of the generated variables
Returns:
The list of the variables in dot notation.
"""
if type(data_object) == list:
for data_line in data_object:
flatten(data_line, key_name=key_name, variable_list=variable_list)
elif type(data_object) == dict:
for k, v in data_object.items():
key_name = f"{key_name}.{k}" if key_name else k
flatten(v, key_name=key_name, variable_list=variable_list)
key_name = ".".join(key_name.split('.')[:-1])
else:
variable_list.append(f"{key_name}: {data_object}" if key_name else None)
return variable_list
def main():
args = parse_args()
if args.debug:
logger.info("Running in debug mode")
logger.setLevel(logging.DEBUG)
with open(args.json_filename) as fp:
json_file = json.load(fp)
json_variables = flatten(json_file)
if args.output_file:
with open(args.output_file, 'w') as fp:
for variable in json_variables:
fp.write(f"{variable}\n")
else:
for variable in json_variables:
print(f"{variable}")
if __name__ == "__main__":
main()