Skip to content

Instantly share code, notes, and snippets.

@curzona
Created May 27, 2015 01:12
Show Gist options
  • Select an option

  • Save curzona/99aa24a5aab62118f14c to your computer and use it in GitHub Desktop.

Select an option

Save curzona/99aa24a5aab62118f14c to your computer and use it in GitHub Desktop.
JSON auto-formatter
import json
import sys
import argparse
parser = argparse.ArgumentParser(prog='jsonformatter')
parser.add_argument('input', type=str, help='Input file')
parser.add_argument('output', type=str, nargs='?', help='Output file')
args = parser.parse_args()
input_file = args.input
output_file = args.output or args.input
text = open(input_file, 'rb').read()
data = json.loads(text)
text = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
open(output_file, 'wb').write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment