made flatten a runable script
This commit is contained in:
parent
e6166ca827
commit
d62e875b99
@ -1,32 +1,87 @@
|
|||||||
def flatten(data_object, key_name=None):
|
#!/usr/bin/env python3
|
||||||
# print('-' * 10)
|
|
||||||
# print(f"data_object = {data_object}, key_name = {key_name}")
|
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:
|
if type(data_object) == list:
|
||||||
for data_line in data_object:
|
for data_line in data_object:
|
||||||
# print(f"dl | data_line = {data_line}")
|
flatten(data_line, key_name=key_name, variable_list=variable_list)
|
||||||
# print(f"dl | key_name = {key_name}")
|
|
||||||
# print(f":recursion")
|
|
||||||
flatten(data_line, key_name=key_name)
|
|
||||||
# print(":returned to the stack")
|
|
||||||
elif type(data_object) == dict:
|
elif type(data_object) == dict:
|
||||||
for k, v in data_object.items():
|
for k, v in data_object.items():
|
||||||
# print(f"dd | k, v = {k}, {v}")
|
|
||||||
# print(f"dd | key_name = {key_name}")
|
|
||||||
key_name = f"{key_name}.{k}" if key_name else k
|
key_name = f"{key_name}.{k}" if key_name else k
|
||||||
# print(f"dd | key_name.k = {key_name}")
|
flatten(v, key_name=key_name, variable_list=variable_list)
|
||||||
# print(f":recursion")
|
|
||||||
flatten(v, key_name=key_name)
|
|
||||||
|
|
||||||
# print(":returned to the stack")
|
|
||||||
# print(f"dd | key_name = {key_name}")
|
|
||||||
key_name = ".".join(key_name.split('.')[:-1])
|
key_name = ".".join(key_name.split('.')[:-1])
|
||||||
# print("...back tracking key_name one level")
|
|
||||||
else:
|
else:
|
||||||
print(f"--> {key_name}: {data_object}") if key_name else None
|
variable_list.append(f"{key_name}: {data_object}" if key_name else None)
|
||||||
|
|
||||||
# print("<< returning from this run")
|
return variable_list
|
||||||
# print('=' * 10)
|
|
||||||
|
|
||||||
return
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user