This short lisp example shows how to define a function that pretty prints your JSON selection in Emacs, based on a standard Python tool. …
My first regards go to the Irreal Blog and the author of Beautifying JSON In Emacs.
My second regards go to Trey Jackson who provided the example for the usage of "shell-command-on-region" (see the Stackoverflow post).
The key element is the "shell-command-on-region" function which calls a Python tool named "json.tool". This small tool beautifies your JSON. The code around is for replacing the selection with the output from the shell command.
USAGE: Mark your JSON text and call M-x pretty-print-json. The selection should be replaced with the beautified JSON.
GET IT: Put the code below to your emacs.el and adjust the path to python.exe. Irreal says Python > 2.6 is required.
WHY? The command provided by Irreal "C-u M-|" is impossible to type on German keyboard layouts. C-u Esc-AltGr-| is too long. Also Python.exe is not in my PATH and I have to type it down by hand (I could have adjusted my path as well) and remember the Python tool name.
;; pretty prints the selection on a json document ;; uses python. ;; adjust the python path and executable. ;; see http://stackoverflow.com/questions/1548605/emacs-lisp-shell-command-on-region (defun pretty-print-json(&optional b e) (interactive "r") (shell-command-on-region b e "c:/Python27/python -m json.tool" (current-buffer) t) )