Table of Contents

JSON layer

Table of ContentsClose


1 Description

This layer adds support for JSON files with json-mode

1.1 Features:

2 Install

To use this configuration layer, add it to your ~/.spacemacs. You will need to add json to the existing dotspacemacs-configuration-layers list in this file.

2.1 Configuration

To define the default indentation set the variable js-indent-level.

2.2 web-beautify

See web-beautify layer documentation.

2.3 prettier

See prettier layer documentation.

2.4 Choosing a formatter

To choose a formatter, set the layer variable json-fmt-tool:

(json :variables json-fmt-tool 'web-beautify)

Formatter can be chosen on a per project basis using directory local variables (files named .dir-locals.el at the root of a project), an example to use the prettier formatter:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((json-mode (json-fmt-tool . prettier)))

Note: you can easily add a directory local variable with SPC f v d.

2.5 Formatting on save

To enable using the selected formatter on save, set the layer variable json-fmt-on-save:

(json :variables json-fmt-on-save t)

3 Usage

3.1 Reformat

SPC m = will reformat the whole buffer or the active region. Use numerical prefix argument to specify a different indentation than js-indent-level. Use the universal prefix argument to print decoded strings, for instance:

{"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>","home":"/home/foobar"}

Will be reformatted:

  "name": "foo\"bar",
  "nick": "foo ä bar",
  "description": "<pre>
  "home": "/home/foobar"

3.2 Display navigable hierarchy

SPC m h h displays the hierarchy for the whole JSON document or the active region. Use the universal prefix argument SPC u SPC m h h to create the hierarchy for the JSON after the point.

4 Key bindings

Key binding Description
SPC m = DWIM Reformat JSON
SPC m h p Get the path of the value at point
SPC m h h DWIM navigate JSON hierarchy
SPC m h H Navigate JSON hierarchy of the whole buffer

4.1 JSON hierarchy

Key binding Description
RET Expand/Collapse node
TAB Select next node
S-TAB Select previous node

Author: root

Created: 2019-11-10 Sun 14:17