Easy JSON formatting

in

Sometimes we want to format some JSON. Here’s an easy way to set up a Service in Automator to make this easier.

Let’s create a service. Open Automator and choose to create a new Service

From the Library, select the “Run Shell Script” action and drag it to the right.

In the action box type:

json_pp | pbcopy

This is what actually does the formatting. json_pp comes with OS X . This runs the selected text through the tool and puts the result in the pasteboard.You can paste your formatted output wherever you like.

Save the action with a name - I’ve called mine JSON Pretty Print. You should see the new service installed under ~/Library/Services

Rather than have to paste the result, we can define another service that replaces the selected text with the result. Create another service as before except that the action doesn’t put the results on the pasteboard.

json_pp

Note that the “Output replaces selected text” option is checked.

Save this action with a name - I’ve called mine JSON Pretty Print In Place.

An advantage of using the service is that it is intelligent about when to make the service available. If there is no selection, then the Service is not listed:

If the selection is in a place where the text cannot be pasted, then only the service that copies the result is presented.

But if the text can be pasted in place, the Service is shown.

You can see a gif of this in action at this link - I’m linking to this so there isn’t a constantly repeating animation while you read this.

Of course, if you are using Emacs, you don’t want to be grabbing the mouse to get to the action, here’s a lisp function I use to format JSON in place:

(defun json-format ()
  "Reformats the JSON in the region for humans."
  (interactive)
  (save-excursion
    (shell-command-on-region (mark) (point) "python -m json.tool" (buffer-name) t)))

Edit

Thanks to Chris Ridd for pointing out that I could use json_pp.