'How to fix JSON indentation in vim?

In vim, the default indentation for JSON is:

{
    "employees": [
    { "firstName":"John" , "lastName":"Doe" }, 
    { "firstName":"Anna" , "lastName":"Smith" }, 
    { "firstName":"Peter" , "lastName":"Jones" }
    ]
}

But what I expect is:

{
    "employees": [
        { "firstName":"John" , "lastName":"Doe" }, 
        { "firstName":"Anna" , "lastName":"Smith" }, 
        { "firstName":"Peter" , "lastName":"Jones" }
    ]
}

I did google and tried some vim-json plugins, but none of them fix this issue.



Solution 1:[1]

Easier way is to just external command as a filter for a selection. e.g.

  1. Make a selection
  2. Type :!python -m json.tool

Solution 2:[2]

romainl recommendation is the preferred way, but sometimes you need to pretty indent JSON text inside some buffer that doesn't have the json filetype. I use this nice command:

command! -range -nargs=0 -bar JsonTool <line1>,<line2>!python -m json.tool

Just run :JsonTool and it will pretty print the current line. It can take a range as well:

:JsonTool
:'<,'>JsonTool
:10,25JsonTool

If you do not have python or prefer a pure vim solution you may be interested in Tim Pope's jdaddy plugin. Jdaddy provides JSON text objects: aj and ij as well as print print JSON formatting, e.g. gqaj.

Solution 3:[3]

You can send to an external tool, as an example, if you have python you can send the content to python's json tool using:

:%!python -m json.tool

Solution 4:[4]

If you have jq (source) available, you can use in the command mode:

:%!jq .

Solution 5:[5]

python -m json.tool reorders the position of the JSON object properties, if you have node installed, you can just use this function:

function FormatJSON(...) 
  let code="\"
        \ var i = process.stdin, d = '';
        \ i.resume();
        \ i.setEncoding('utf8');
        \ i.on('data', function(data) { d += data; });
        \ i.on('end', function() {
        \     console.log(JSON.stringify(JSON.parse(d), null, 
        \ " . (a:0 ? a:1 ? a:1 : 2 : 2) . "));
        \ });\""
  execute "%! node -e " . code 
endfunction

Mapped to f-j in .vimrc

nmap fj :<C-U>call FormatJSON(v:count)<CR>

You can also pass a number of spaces for a tab, 2 are the default if you don't specify any.

4fj

My complete .vimrc is here https://github.com/botverse/.dotfiles/blob/master/.vimrc

Solution 6:[6]

gg=G is what you need if you are using vim.

Solution 7:[7]

Here's an example in Ruby:

:%! ruby -rjson -e "print JSON.pretty_generate(JSON.parse(ARGF.read))"

(https://gist.github.com/zinovyev/c4b6ec3c24670278adfebfb9ecced84b)

Solution 8:[8]

Add this statement to /etc/vim/vimrc:

autocmd Filetype json setlocal ts=2 sw=2 expandtab

It will force json presentation with 2 spaces tab, besides defining tab behaviour for such file.

Solution 9:[9]

You can use python to format json:

:%!python -m json.tool

Besides, there are several other tools can be used, such as jsbeautify, prettydiff,prettier,jq and fixjson.

See https://medium.com/@codevalues/how-to-explore-a-json-with-thousands-of-lines-using-vim-7f9d1ec550dc for more details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rnevius
Solution 2
Solution 3 Reut Sharabani
Solution 4 gabra
Solution 5 Alfonso de la Osa
Solution 6 DawnSong
Solution 7 zinovyev
Solution 8 ivanleoncz
Solution 9 CodeValues