This is a Neovim-only plugin containing a function to return a path to access the value under the cursor by using treesitter. It may help you understand how to access even deeply nested values in JSON files.
A "JSON path" is a jq-like expression, such
as .
for root, .[0]
for the first array item, .name
for the property
"name" of an object.
jsonpath-nvim-demo.webm
Colorscheme is vim-moonfly-colors
Using lua:
-- in after/ftplugin/json.lua
-- show json path in the winbar
if vim.fn.exists("+winbar") == 1 then
vim.opt_local.winbar = "%{%v:lua.require'jsonpath'.get()%}"
end
-- send json path to clipboard
vim.keymap.set("n", "y<C-p>", function()
vim.fn.setreg("+", require("jsonpath").get())
end, { desc = "copy json path", buffer = true })
Using vim:
" in after/ftplugin/json.vim
" show json path in the winbar
if exists('+winbar')
setlocal winbar=%{luaeval('require\"jsonpath\".get()')}
endif
" send json path to clipboard
nnoremap <buffer> y<C-p> :let @+=luaeval('require"jsonpath".get()')<CR>