Json

From WikEmacs
Jump to: navigation, search

Let's look at what we have at our disposal to work with json in Emacs, both such as a user (editing json files) and as an elisp developper.

Editing and working in json files[edit]

json-mode[edit]

The json-mode json-mode (in MELPA and MELPA stable) offers:

  • syntax highlighting and indentation,
  • format the region or buffer, with json-reformat. Bound to C-c C-f.
  • display the path of the object at point, with json-snatcher. Bound to C-c C-p.

It actually inherits from js-mode to add better syntax highlighting for JSON.

Format json[edit]

Starting from Emacs 24.4 we have the built-in commands json-pretty-print and json-pretty-print-buffer.

Anyways there also is the json-reformat package, in MELPA.

web-beautify can also format html, css and javascript, based on the js-beautify package on the npm registry.

flymake-json[edit]

flymake-json is a Flymake handler for json using "jsonlint" from the npm package registry.

See also flycheck-demjsonlint to be able to specify linting rules.

x-path-walker - Navigate json/htm/xml based on the elements' path[edit]

x-path-walker (in MELPA) offers an Helm interface to ease the navigation of json files.

json-navigator - navigate json presented as a tree[edit]

json-navigator (in MELPA, for Emacs >= 25.1 only) allows to display any JSON document as a tree, which leafs you can unfold and follow.

It is an example of the hierarchy project.

Json-navigator.gif

json-snatcher - get the path of a json element[edit]

See json-snatcher:

Json-snatcher.gif

Development[edit]

Parsing and generating json[edit]

With the built-in json.el library.

See also[edit]

Request json content[edit]

See request.el that allows to request content to http endpoints and parse the result to json.

Using js2-mode functions[edit]

We could also use js2-mode functions, even when the mode itself isn't activated. We then have js2-print-json-path at our disposal as an alternative to json-snatcher. See the custom function on redguardtoo's blog post.