Last active
August 29, 2015 14:16
-
-
Save hartfordfive/32508c244cb50680b2bc to your computer and use it in GitHub Desktop.
Comparing two JSON files
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,hashlib | |
| try: | |
| with open(sys.argv[1], 'r') as content_file_orig: | |
| content_original = content_file_orig.read() | |
| json_orig = json.loads(content_original) | |
| except Exception, e: | |
| print "Error loading JSON file {0}! Please check it's validity.".format(sys.argv[1]) | |
| sys.exit(1) | |
| try: | |
| with open(sys.argv[2], 'r') as content_file_new: | |
| content_new = content_file_new.read() | |
| json_new = json.loads(content_new) | |
| except Exception, e: | |
| print "Error loading JSON file {0}! Please check it's validity.".format(sys.argv[1]) | |
| sys.exit(1) | |
| hash_orig = hashlib.sha1(json.dumps(json_orig, sort_keys=True)).hexdigest() | |
| hash_new = hashlib.sha1(json.dumps(json_new, sort_keys=True)).hexdigest() | |
| same_values = 'YES' if (hash_orig == hash_new) else 'NO' | |
| print "\n{:#^80}".format(" JSON File Comparison ") | |
| print "File #1: {0}\nFile #2: {1}".format(sys.argv[1], sys.argv[2]) | |
| print "Files have the same elements?: {0}".format(same_values) | |
| print "{:-^80}".format("") | |
| print "SHA1 hash of sorted original JSON: {0}".format( hash_orig ) | |
| print "SHA1 hash of sorted new JSON: {0}\n".format( hash_new ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment