Published May 10, 2020
Here's a simple Python script to pretty-print a JSON file:
#!/usr/bin/env python
from __future__ import print_function
import sys
import json
try:
json.dump(json.load(sys.stdin), sys.stdout, sort_keys=True, indent=4)
except ValueError as err:
print("Error:", err, file=sys.stderr)
exit(1)
To format JSON from the pasteboard:
$ pbpaste | ppjson
To format JSON in a file:
$ ppjson < filename
Both with print the formatted JSON to stdout. To write to a file, you can redirect the output like this:
$ ppjson < input_filename > output_filename
Or, you could format JSON from the pasteboard (using pbpaste
piped through ppjson
) the copy the formatted JSON back to the pasteboard (using pbcopy
):
$ pbpaste | ppjson | pbcopy