Format JSON in vim using Python’s json.tool module
Dec 20th, 2010 by RealNitro
Based on this blogpost, use this Vim command to format the contents of a JSON file:
:%!python -m json.tool
That is all.
Dec 20th, 2010 by RealNitro
Based on this blogpost, use this Vim command to format the contents of a JSON file:
:%!python -m json.tool
That is all.
thanks for the post! I added the following to my .vimrc, and now all I have to do to format JSON is select it in visual mode and hit \-j:
” format JSON
map j !python -m json.tool
Ugh, the dog ate my html - should be:
” format JSON
map <Leader>j !python -m json.tool<CR>
Adrian: thx for the tip!
@Adrian:
It still ate the colon and percentage sign. These should be prefixed in front of the exclamation mark.
That’s awesome. Is there a python -m xml.tool?
There seems to be no xml formatting ‘tool’ in Python. You could use something like xmllint?
You can use “%!tidy -xml -q -i” to do xml formatting