Emacs mode to edit JSON

js-mode supports syntax highlighting and indentation for json files.

This is as of Emacs 23.2, when espresso-mode was incorporated into Emacs and renamed js-mode.

Check it out: http://www.nongnu.org/espresso/


Have you tried Steve Yegge's js2-mode for Emacs?


If you want something lightweight try this major-mode I hacked together: https://github.com/joshwnj/json-mode

It's actually no more than some extra syntax highlighting on top of javascript-mode, but for my purposes I've found it to work quite well.

Another common use-case is auto-formatting a JSON file (eg. if it's whitespace-compressed and you want more readability). To do this I'm just piping the buffer through a command-line script: C-u M-|


+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.