17

I am trying to edit a Swagger JSON file in emacs. It contains large nested JSON objects, so I want to collapse and hide some of them.

I have tried origami-mode, configured as origami-c-style-parser in origami-parser-alist, but it is quite unsophisticated (the word "parser" seems to be an overly grandiose word for what it does). It doesn't notice that Swagger URL parameter substrings like {id} are inside JSON strings, so it happily collapses them too when I ask it to collapse all nodes in the buffer except the current node. I don't want it to do that. I want a way of collapsing only JSON subtrees like objects, not substrings of JSON strings that happen to have curly braces in them. How can I achieve this goal?

3
  • I don't know this for sure, but it is quite possible that js2-mode has folding that can also fold JSON files.
    – wvxvw
    Commented Jul 12, 2016 at 5:55
  • Related: Code-folding extensions for json.
    – Dan
    Commented Jul 12, 2016 at 15:48
  • One approach is to convert your json into some similar tree format and operate on that. This can convert into xml github.com/hay/xml2json. Yaml would be another approach.
    – Att Righ
    Commented Feb 15, 2017 at 18:43

2 Answers 2

2

Emacs comes with hs-minor-mode which can be used selectively hide/show code and comment blocks in several languages, including JavaScript. By default this will let you hide and show JSON blocks delimited by '{' and '}' but it is easily modified to allow '[' and ']' as block delimeters as well. To do this we need to modify the js-mode entry in hs-special-modes-alist with something like:

(setcdr (assoc 'js-mode hs-special-modes-alist) '("[{[]" "[}\\]]" "/[*/]" nil))

You should then find that [...] blocks can be hidden and shown as well as {...} blocks.

1
  • Using this answer, my collapsed sections are displayed as {... and [... (without closing brackets and curlies), as opposed to {...} and [...]. +1 anyway, because I don't really "need" to see the closing delimiter in the collapsed view. Commented Nov 29 at 11:05
-1

My json-mode has a code folding feature which shouldn't try to fold things that look like Objects or Arrays inside of strings. It's not a mature major-mode, but it should be usable.

1
  • -1, Linked Github page itself now advises against using the repo. Commented Nov 29 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.