Three Ways To Indent JSON
Python comes with a JSON encoder and decoder you can use to prettify messy JSON code.
On the Command Line
If you have an unindented JSON file:
cat ugly.json | python -mjson.tool > pretty.json
(May I recommend the wonderful DTerm to run commands from almost any app you might be in?)
From within VIM
Using filter commands, !
, replace the text with its formatted version, filtered through the same Python command:
:%!python -mjson.tool
Since it’s VIM, you can replace as little or as much as needed; see Use filter commands to process text for more information.
With a Service
Create an Automator service and use the Run Shell Script action to process the input text (make sure to select the Output replaces selected text checkbox), once again using Python:
Since Automator automatically saves services to the ~/Library/Services/
directory, it’ll become available to you instantly, and it’ll allow you to go from this:
to this: