Skip to content

Instantly share code, notes, and snippets.

@ashelly
Created September 5, 2014 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashelly/1052273b62c180d3060e to your computer and use it in GitHub Desktop.
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.
#!/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