Does anybody know a good Emacs mode to edit JSON? An app I am working on uses a JSON based communication protocol and having the data nicely indented and syntax-highlighted would help me a lot in the process of figuring it out.
-
see http://emacs.stackexchange.com/questions/24855/is-there-a-way-to-check-whether-or-not-a-string-is-valid-json/ I use js2-mode to validate JSON – chen bin Aug 07 '16 at 14:04
12 Answers
+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.
-
fwiw, I forked'd Josh's json-mode and added jstevenco's addition to it at https://github.com/chad3814/json-mode – chad May 10 '12 at 14:00
-
I have requested a fix for the non-ASCII udate at https://github.com/joshwnj/json-mode/issues/5 – Jarl Nov 26 '12 at 11:44
-
The latest version of json-mode has now fixed the utf-8 issue: see https://github.com/joshwnj/json-mode/issues/5 – Jarl Jan 04 '13 at 07:38
-
1This all works out the box installing the latest [json-mode package](http://melpa.org/#/json-mode) on [melpa](http://melpa.org/#/). Nice work guys. – TooTone Feb 17 '15 at 16:03
-
do not use `"unicode_escape"` with json content. [Use `ensure_ascii=False` instead.](http://stackoverflow.com/q/18337407/4279) – jfs Apr 18 '15 at 03:55
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/

- 4,033
- 5
- 32
- 29
-
6Nice! I put `(setq auto-mode-alist (cons '("\\.json\\'" . js-mode) auto-mode-alist))` in my `.emacs` – Marvin Sep 06 '12 at 12:55
-
1`(add-to-list 'auto-mode-alist '("\\.json$" . js-mode))` is the same thing but a little shorter. – ksrb Aug 08 '16 at 20:45
Have you tried Steve Yegge's js2-mode for Emacs?

- 399,953
- 195
- 994
- 1,670
-
16JSON is not really supported by js2-mode, though. http://code.google.com/p/js2-mode/issues/detail?id=50 – viam0Zah Apr 18 '09 at 00:48
-
1
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-|

- 716
- 7
- 7
-
2fwiw, I forked this repo and added jstevenco's beautify-json function, and it's at https://github.com/chad3814/json-mode - also sent a pull request. – chad May 10 '12 at 14:04
-
-
FWIW Josh accepted Chad's pull request to include jstevenco's addition (got all that?) so as of this writing [Josh's json-mode repo on github](https://github.com/joshwnj/json-mode) is the most up-to-date source for json-mode. – Steven Collins Aug 06 '12 at 17:47
-
Is this a bug or am I doing something wrong? http://emacs.stackexchange.com/q/17319/4003 (I load json-mode in my .emacs) – Marcus Junius Brutus Oct 12 '15 at 19:46
I've prepared a workaround for js2-mode so it parses json files without errors. You can find it in my comment: http://code.google.com/p/js2-mode/issues/detail?id=50#c7
(I wanted to post it as a comment do J.F. Sebastian solution, but it seems I'm not allowed to do so (no 'add comment' link))

- 32,050
- 5
- 35
- 37
json.el
by Edward O'Connor is part of GNU Emacs since 23.1 (2008).
While it isn't a syntax highlighter, it has a useful function to format JSON:
M-x json-pretty-print-buffer RET
So, if you have a recent version of Emacs, there is no need for jq
or python -m json.tool
.
-
1Using `json-pretty-print-buffer` included with GNU Emacs 24.5.1, I have found that while it nicely formats my JSON, it also reverses its content. Fortunately, I also found that running it a second time on my JSON restores it to its original order. – DavidRR Feb 07 '18 at 19:18
Since JSON is a subset of YAML, yaml-mode
works too (I don't know how it compares to js-mode
and json-mode
, though).
Install (from emacs): M-x package-install yaml-mode
.
Association of yaml-mode
with YAML and JSON files, in ~/.emacs.d/init.el
:
(add-to-list 'auto-mode-alist '("\\.yaml$" . yaml-mode))
(add-to-list 'auto-mode-alist '("\\.json$" . yaml-mode))

- 91,433
- 48
- 218
- 260
JSON is supported by espresso-mode

- 151
- 2
-
2I can only get espresso-mode to work with legal Javascript syntax. JSON expressions don't, for example, get indented properly. – ESV Feb 05 '10 at 04:51
-
Espresso has been incorporated into GNU Emacs starting with version 23.2 and has been renamed js-mode. espresso-mode will no longer be maintained outside of Emacs proper. – Jeff Bauer Nov 10 '14 at 02:00
js3-mode:https://github.com/thomblake/js3-mode
js3-mode is an improved js2-mode
This package can be installed by package-list-packages command

- 1
- 1
I will also second Josh's json-mode, but also recommend flymake-json as an addition. It helps highlight syntax errors.
I don't like using python -mjson.tool
because it reorders items in JSON objects.
I find (prog-indent-sexp)
works just fine to reindent, and using jsonlint
instead of python -mjson.tool
works for pretty printing/reformatting in beautify-json
(eval-after-load "json-mode"
'(progn
(require 'flymake-json)
;; flymake-cursor displays error in minibuffer message area instead of requiring hover
(require 'flymake-cursor)
(add-hook 'json-mode-hook 'flymake-json-load)
(define-key json-mode-map "\C-c\C-n" (function flymake-goto-next-error))
)
)

- 4,930
- 1
- 34
- 37
I've expanded on Mariusz Nowak's workaround, to make it usable as a major mode in its own right. Little modification was required beyond simply deriving the mode; the only change Nowak's work actually needed was the ability to recognize buffers not associated with files, or associated with files whose names don't end in .json
, as JSON, which we accomplish with a buffer-local variable.
Here's the augmented workaround:
(make-variable-buffer-local 'js2-parse-as-json)
(defadvice js2-reparse (before json)
(setq js2-buffer-file-name buffer-file-name))
(ad-activate 'js2-reparse)
(defadvice js2-parse-statement (around json)
(if (and (= tt js2-LC)
js2-buffer-file-name
(or js2-parse-as-json
(string-equal (substring js2-buffer-file-name -5) ".json"))
(eq (+ (save-excursion
(goto-char (point-min))
(back-to-indentation)
(while (eolp)
(next-line)
(back-to-indentation))
(point)) 1) js2-ts-cursor))
(setq ad-return-value (js2-parse-assign-expr))
ad-do-it))
(ad-activate 'js2-parse-statement)
(define-derived-mode json-mode js2-mode "JSON"
"Major mode for editing JSON data."
:group 'json
(setq js2-parse-as-json t)
(js2-reparse t))
(add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
If you already use js2-mode, this may be a better option than js-mode
plus flymake-json
because you need not install anything new (js2-mode already does syntax checking, no need for an external tool), and because this mode will inherit your js2-mode configuration, which js-mode will not.

- 1
- 1

- 3,692
- 1
- 19
- 26
I would also recommand js2-mode.
JSON stands for JavaScript Object Notation. It's not another language and it's even not a data container like yaml or xml are. JSON could be used as a data container if there's no function (or in this case we should say method) inside a JSON object, but it's not the primary goal of JSON :-)
var myJSObject = {
attr: {foo: "bar", baz: ["quux", "truc", "pouet"]},
fooAlert: function (num) {
alert(this.attr.foo+' '+num);
}
};
myJSObject.fooAlert(42);

- 3,846
- 1
- 22
- 21
-
10To be exact, the example you provided ain't JSON but the JavaScript object literal, which is similar but different. – viam0Zah Apr 17 '09 at 23:44
-
6Another language that is a data container like YAML or XML is exactly what JSON is, actually, and JSON does not allow functions. – Kragen Javier Sitaker Apr 15 '11 at 18:01