Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs mode to edit JSON

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.

like image 914
Ryszard Szopa Avatar asked Jan 12 '09 15:01

Ryszard Szopa


People also ask

How do I make a JSON file editable?

Procedure. In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.

What is JSON mode?

el. Major mode for editing JSON files. Extends the builtin js-mode to add better syntax highlighting for JSON and some nice editing keybindings.

Can you edit a JSON file?

JSON is a plain text file that can be opened in a text editor. You can easily modify and save it back without any special software.

What does JSON stand for?

JavaScript Object Notation, more commonly known by the acronym JSON, is an open data interchange format that is both human and machine-readable. Despite the name JavaScript Object Notation, JSON is independent of any programming language and is a common API output in a wide variety of applications.


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

like image 192
jstevenco Avatar answered Sep 29 '22 23:09

jstevenco


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/

like image 27
Steve Avatar answered Sep 29 '22 23:09

Steve