Skip to content

Instantly share code, notes, and snippets.

@re5et
Created February 28, 2013 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save re5et/5053399 to your computer and use it in GitHub Desktop.
Save re5et/5053399 to your computer and use it in GitHub Desktop.
(defun prettify-json ()
"NOTE: requires ruby in $PATH. Replace a valid json region with
pretty printed json By default uses the jj method to print,
uses pp if there is a prefix argument"
(interactive)
(let* ((print-method (if current-prefix-arg "pp" "jj"))
(cmd (format
"ruby -e 'require \"json\"; require \"pp\"; %s JSON.parse(gets)'" print-method)))
(shell-command-on-region
(region-beginning)
(region-end)
cmd nil t)))
@allenluce-zz
Copy link

Can also:

(defun prettify-json (start end)
  "NOTE: requires ruby in $PATH. Replace a valid json region with
pretty printed json By default uses the jj method to print,
uses pp if there is a prefix argument"
  (interactive "r")
  (let* ((print-method (if current-prefix-arg "pp" "jj"))
         (cmd (format
               "ruby -e 'require \"json\"; require \"pp\"; %s JSON.parse(gets)'" print-method)))
    (shell-command-on-region start end cmd nil t)))

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