Last active
December 15, 2015 12:49
-
-
Save carlosefonseca/5263312 to your computer and use it in GitHub Desktop.
Formats and orders the keys in JSON data. If input is stdin, it will colorize the data via pygmentize (if available) and output to stdout. --no-color can be passed as argument to skip pygmentize.
If a path is passed as argument, it will override the file with a formated version.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
import codecs | |
import sys | |
import locale | |
from subprocess import Popen, PIPE, STDOUT | |
import argparse | |
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout); | |
# ARG PARSER | |
parser = argparse.ArgumentParser(description='Makes JSON readable.') | |
parser.add_argument("--no-color", dest="color", action="store_false", default=True, help="Don't pygmentize the output.") | |
parser.add_argument('infile', nargs='?', default=sys.stdin) | |
args = parser.parse_args() | |
isFile = type(args.infile) != file | |
print "isFile:",isFile | |
j = "" | |
# READ | |
if isFile: | |
with codecs.open(args.infile, "r", "utf-8") as f: | |
print "loading file" | |
j = json.loads(f.read()) | |
else: | |
print "loading stdin" | |
data = sys.stdin.read() | |
if (len(data) == 0): | |
# print sys.stdout.encoding | |
# line = u"\u0411" | |
# sys.stdout.write(line) | |
# print line | |
print u"No data \uD83D\uDCA9" | |
sys.exit() | |
j = json.loads(data) | |
# PROCESS | |
jj = json.dumps(j,sort_keys=True, indent=4) | |
# WRITE | |
if isFile: | |
with codecs.open(args.infile, "w+", "utf-8") as f: | |
f.write(jj) | |
else: | |
if args.color: | |
try: | |
cmd = "pygmentize -O style=monokai -l json".split(" ") | |
p = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=STDOUT) | |
grep_stdout = p.communicate(input=jj)[0] | |
print(grep_stdout) | |
except Exception, e: | |
print jj | |
else: | |
print jj |
Added argparse, --no-color, outputs colorless if pygmentize fails.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a bash function that takes an URL, calls curl and sends the resulting data to this script. Very useful for quick and dirty REST testing API's that output unformatted data.