Skip to content

Instantly share code, notes, and snippets.

@erjiang
Created March 13, 2012 15:56
Show Gist options
  • Save erjiang/2029554 to your computer and use it in GitHub Desktop.
Save erjiang/2029554 to your computer and use it in GitHub Desktop.
jsoncheck
#!/usr/bin/env python
"""jsoncheck checks a list of filenames to see if they are valid json files.
Usage:
jsoncheck file1 [file2 file3 ...]
jsoncheck returns 0 if all files loaded successfully, and 1 otherwise.
"""
import json
import sys
if len(sys.argv) < 2:
print "Usage: jsoncheck filename [...]"
returncode = 0
filelist = sys.argv[1:]
for filename in filelist:
try:
handle = open(filename, 'r')
except IOError as (_, strerror):
print "File error:", strerror
returncode = 1
continue
try:
json.load(handle)
except ValueError, e:
print "Failed to parse", filename+":", e
returncode = 1
continue
print filename, "loaded successfully"
exit(returncode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment