An OS X Automator Service for Reformatting JSON Text

Note: This is an old post. I now use jq (http://stedolan.github.io/jq/) instead of the formatjson Node script described below, and I recommend that you do too.

I had some JSON files that were not indented consistently. I edit these files by hand, so I wanted a way to easily reformat them.

My text editor has a reformatting command, but I really hate what it produces, so I decided to make my own JSON reformatting service for OS X that I could use in any application.

First, I wrote a Node.js script to do the reformatting:

(Also available as Literate CoffeeScript).

I saved that file as ~/bin/formatjson, and made it executable with chmod +x ~/bin/formatjson.

Then I used Automator to create a service:

  1. Launch Automator
  2. Create a new Automator document of type Service
  3. Set it to "Service receives selected text in any application", and check the Output replaces selection text box.
  4. Drag a Run Shell Script action into the workflow, and enter the following command:
  5. PATH=$PATH:~/bin:/usr/local/bin formatjson
  6. Save the service with the name "Reformat JSON Text".

(Note: In step 4, if your node executable is not in /usr/local/bin, then substitute the appropriate directory. Also, if you saved formatjson to a directory other than ~/bin, substitute the appropriate directory for that.)

Automator screenshot

Now, whenever I want to reformat some JSON in a text editor, I just select it, right-click, and choose Services > Reformat JSON Text.

And when I want to reformat things from the command line, this works:

formatjson <ugly.json >pretty.json

If you'd rather have a web page that does this, see A Web Page for Reformatting JSON Text.

© 2003-2023 Kristopher Johnson