Created
January 5, 2016 01:11
-
-
Save tekton/07de23fee3b60c2807e2 to your computer and use it in GitHub Desktop.
Yet Another JSON Pretty Print
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 ujson as json | |
import argparse | |
import logging | |
logger = logging.getLogger(__name__) | |
def get_json(fn): | |
try: | |
with open(fn) as f: | |
return json.load(f) | |
except Exception as e: | |
logger.error(e) | |
return False | |
def pprint_json(of, j): | |
try: | |
with open(of, "w") as f: | |
t = json.dumps(j, indent=4) | |
f.write(t) | |
return True | |
except Exception as e: | |
logger.error(e) | |
return False | |
if __name__ == "__main__": | |
""" | |
First argument is input file, second is output file | |
""" | |
parser = argparse.ArgumentParser(description='Pretty up some json') | |
parser.add_argument('I', metavar='INPUT', type=str, help="Input file") | |
parser.add_argument('O', metavar='OUTPUT', type=str, help="Output file") | |
args = parser.parse_args() | |
try: | |
j = get_json(args.I) | |
pprint_json(args.O, j) | |
except Exception as e: | |
logger.error(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment