Created
March 13, 2012 15:56
-
-
Save erjiang/2029554 to your computer and use it in GitHub Desktop.
jsoncheck
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 | |
"""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