Sheharyar Naseer

Prettify JSON in Vim with jq


I find myself often pasting in some JSON in Vim to modify it or just read it. But more often than not it’s not formatted / pretty-printed, so I have this little custom command that does it for me.

For formatting the JSON itself, I use jq, a super light-weight and powerful JSON processor. jq actually does a lot more than formatting json, but for the purposes of this post will stick with that.

To install jq on macOS:

$ brew install jq

Once installed, simply calling it on any json data will automatically format it:

$ cat data.json | jq .

So the command in Vim is pretty simple too:

command FormatJSON %!jq .

Calling it in any buffer with json content will automatically format and prettify it. You can find this in my Vim dotfiles.


If you prefer not to download an extra package just for formatting json, and you already have python installed (you probably do), you can use the built-in python json tool for this as well:

command FormatJSON %!python -m json.tool