user2309998
user2309998

Reputation: 727

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.

Upvotes: 41

Views: 19179

Answers (10)

It's similar to @ivanleoncz (the best and simplest one) but with some extras (also put on /etc/vim/vimrc or ~/.vimrc):

au! BufNewFile,BufReadPost *.{json} set filetype=json foldmethod=indent
autocmd FileType json setlocal ts=2 sts=2 sw=2 expandtab modeline autoindent smartindent

Upvotes: 0

CodeValues
CodeValues

Reputation: 1

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.

Upvotes: 0

ivanleoncz
ivanleoncz

Reputation: 10015

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.

Upvotes: 1

gabra
gabra

Reputation: 10564

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

:%!jq .

Upvotes: 16

awesome_person
awesome_person

Reputation: 1263

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

Upvotes: 47

zinovyev
zinovyev

Reputation: 2163

Here's an example in Ruby:

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

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

Upvotes: 1

DawnSong
DawnSong

Reputation: 5162

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

Upvotes: 4

Alfonso de la Osa
Alfonso de la Osa

Reputation: 1574

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

Upvotes: 7

Reut Sharabani
Reut Sharabani

Reputation: 31339

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

Upvotes: 16

Peter Rincker
Peter Rincker

Reputation: 45117

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.

Upvotes: 26

Related Questions