If you are using nvim and nvim-treesitter. You can also use treesitter to fold JSON files.
Config for nvim-treesitter:
require("nvim-treesitter.configs").setup {
ensure_installed = { "python", "cpp", "lua", "vim", "json" },
ignore_install = {}, -- List of parsers to ignore installing
highlight = {
enable = true, -- false will disable the whole extension
disable = { 'help' }, -- list of language that will be disabled
},
}
Make sure that json parser for treesitter is installed.
Then create after/ftplugin/json.vim
with following config:
set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr()
Open a valid json file, you should be able to see the files are folded properly.
Ref:
https://github.com/nvim-treesitter/nvim-treesitter/issues/1337#issuecomment-864205387