Skip to content

Instantly share code, notes, and snippets.

@drocamor
Created March 21, 2013 14:29
Show Gist options
  • Save drocamor/5213443 to your computer and use it in GitHub Desktop.
Save drocamor/5213443 to your computer and use it in GitHub Desktop.
A function to use json.tool to beautify JSON.
;; beautify json
(defun beautify-json ()
(interactive)
(let ((b (if mark-active (min (point) (mark)) (point-min)))
(e (if mark-active (max (point) (mark)) (point-max))))
(shell-command-on-region b e
"python -mjson.tool" (current-buffer) t)))
@mrinaudo
Copy link

To avoid trailing white spaces on lines whose last character is e.g. a comma, maybe the last line of code can be changed as follows:

  "python -mjson.tool | sed -e 's/ $//'" (current-buffer) t)))

This pipes mjson.tool's output into `sed' to remove white spaces at the end of lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment