Created
September 5, 2014 02:40
-
-
Save ashelly/1052273b62c180d3060e to your computer and use it in GitHub Desktop.
Super simple validator for JSON files. Shows line and column of error, launches editor at that point.
This file contains hidden or 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/python | |
import sys,json,re,subprocess | |
try: | |
file = open(sys.argv[1]) | |
l = file.read() | |
try: | |
json.loads(l) | |
print "OK" | |
except Exception as e: | |
print e | |
m = re.search("line (\d*) column (\d*)",str(e)) | |
if m: | |
ln = int(m.group(1)) | |
cn = int(m.group(2)) | |
print l.split('\n')[ln-1] | |
print ' '*(cn-1)+'^' | |
do_open = raw_input("Open in Editor? (y/N)") | |
if 'y' in do_open.lower(): | |
subprocess.call(['nano', '+{},{}'.format(ln,cn), sys.argv[1]]) | |
except Exception as e: | |
print e | |
print "usage: {} filename_to_validate".format(__file__) | |
exit(-1) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment