Files
google-apis-rs/etc/bin/sort_json_file.py
Guy Taylor 7467f81594 Sort JSON files to minimise git diffs
Sort and format JSON files to remove/minimise any diffs between
update-json runs. This also acts as a JSON validator to error when the
downloaded JSON file has been truncated.
2019-07-03 18:21:35 +08:00

30 lines
892 B
Python
Executable File

#!/usr/bin/env python
# Sort JSON file and print in fixed format
# This lowers git diffs when a file has only changed by whitespace.
import argparse
import json
import os
def main(json_file_path, skip_missing_file):
if not os.path.isfile(json_file_path) and skip_missing_file:
return
with open(json_file_path, 'r') as fh:
loaded_json = json.load(fh)
with open(json_file_path, 'w') as fh:
json.dump(loaded_json, fh, indent=4, sort_keys=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Sort and format JSON file.')
parser.add_argument('json_file', metavar='FILE', type=str, help='JSON file to sort in place.')
parser.add_argument('--skip-missing-file', default=False, action='store_true', help='Do not fail on missing file.')
args = parser.parse_args()
main(args.json_file, args.skip_missing_file)