1

I have a large JSON file that I am largely familiar with, but I have to jump around the file in VIM quite a bit and tweak options and such.

I was wondering if there is a VIM plugin available that allows me to specify a JSONPath / jq / JMESPath path on the search command line to easily jump to a specific key/value object in the file?

I found a comparable plugin for xpath in XML files: https://github.com/actionshrimp/vim-xpath , but the JSON-based plugins I have found only deal with syntax highlighting, linting, pretty-printing/formatting, etc.

Ideally, I'd like to just be able to use JSON Path notation (or the like) to easily jump to the spot in my JSON file I need to read/modify/etc.

2
  • This SO answer uses the input_line_number filter of jq, but this seems to provide the matching line numbers only for multiple single line JSON strings input. Are you working on a single indented JSON file? If you can output the line of the intended match with any of these tools, getting Vim's cursor to jump at it is trivial. I don't know about xpath capabilities but live evaluation might require a more consequent script.
    – LEI
    Mar 26, 2018 at 20:57
  • Yes, usually, the JSON files I am working on are two or four spaced indented pretty-printed JSON files. Thanks for the SO answer. That's an interesting point, if it worked. Unfortunately, I am experiencing the same issue as github.com/stedolan/jq/issues/1193 where the output is the total number of lines and not the line that the filter occurs at.
    – bbbco
    Mar 26, 2018 at 21:54

2 Answers 2

3

I recently wrote a plugin for this called vim-jsonpath. It currently provides the following (mappable) commands:

  • :JsonPath: Echoes the path to the identifier under the cursor.
  • :JsonPath path.to.prop: Searches the active buffer for the given path, placing the cursor on it if found.
1

As an alternative you can use something like gron or jsonpipe to create paths next to the content.

An example of what gron output would look like:

json[0].commit.author = {};
json[0].commit.author.date = "2016-07-02T10:51:21Z";
json[0].commit.author.email = "[email protected]";
json[0].commit.author.name = "Tom Hudson";

You can do your searching and editing then undo this via %!gron --ungron.

4
  • Thanks! I just discovered gron yesterday. The only thing is, it doesn't output the line numbers of where each object lives, which would inform VIM of which line to jump to. I'm opening an issue on gron here: github.com/tomnomnom/gron/issues/33
    – bbbco
    Apr 3, 2018 at 16:07
  • 1
    The author of gron got back to me. He indicated that the Go JSON library doesn't provide this information, and that in order to do so, he'd have to write a custom JSON parser (which he is unwilling to do at this point). So back to the drawing board (unless you have another idea of how the gron solution would still work without specifying the line numbers.
    – bbbco
    Apr 4, 2018 at 13:51
  • Honestly, I gave gron as an alternative, because you can find the component you want, modify it, and then grun --ungron your text back to json. I do not know of a json path finding tool. If you find one please answer your own question Apr 4, 2018 at 14:12
  • I’ve been using gron for git diffs of json, but this is brilliant. I hate the way JSON works in an edit buffer (if I’m just viewing/slicing it I tend to go for :JqPlay from some plugin)
    – D. Ben Knoble
    Nov 2 at 1:20

Your Answer

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

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