Emacs mode to edit JSON

+1 for Josh’s json-mode — works well for me. I added

(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 -m json.tool" (current-buffer) t)))

and

(define-key json-mode-map (kbd "C-c C-f") 'beautify-json)

to json-mode.el to make the shell command invocation easier.

UPDATE: For those of you with a need/desire to do this with unicode, see my question here. The upshot is rather than using:

python -m json.tool

you will want to use

python -c 'import sys,json; data=json.loads(sys.stdin.read()); print json.dumps(data,sort_keys=True,indent=4).decode("unicode_escape").encode("utf8","replace")'

This both beautifies the JSON as well as preserving the original Unicode content.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)