Format JSON in Vim

Occasionally I find myself needing to format a JSON document. There a couple tools online that can do it but it’s nicer do be able to do it right inside of Vim without having to leave. Here’s a simple command to do this (you’ll need Python installed).

Just execute the following with the JSON document open.

%!python -m json.tool

Now if you’re like me you won’t remember this in an hour so you can add it to your .vimrc file as a custom command like so:

command! FormatJSON %!python -m json.tool

And there you go. Easy formatting of JSON documents.