format-json 706 Bytes
Newer Older
1 2 3
#!/usr/bin/env python
r"""Command-line tool to format software release JSON for slapos.

4 5
Inspired by json.tool from python, but enforcing 2 spaces and non-sorted keys.
The files are modified in-place.
6 7 8

Usage::

9
    format-json file1.json [file2.json]
10 11 12 13 14 15 16 17 18 19

"""

import os
import sys
import json
import collections


def main():
20 21 22 23 24 25 26 27 28 29
  for f in sys.argv[1:]:
    with open(f, 'rb') as infile:
      try:
        obj = json.load(infile, object_pairs_hook=collections.OrderedDict)
      except ValueError as e:
        raise SystemExit(e)

    with open(f, 'wb') as outfile:
      json.dump(obj, outfile, sort_keys=False, indent=2, separators=(',', ': '))
      outfile.write('\n')
30 31 32

if __name__ == '__main__':
  main()