Is there an editor or tool for Linux command line to format JSON data?
4 Answers 4
alias pp='python -mjson.tool'
pp mydata.json
From the first link in the accepted answer: http://ruslanspivak.com/2010/10/12/pretty-print-json-from-the-command-line/
Modern distros may require python3
instead of python
.
jq is a lightweight and flexible command-line JSON processor.
jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
jq is written in portable C, and it has zero runtime dependencies. You can download a single binary, scp it to a far away machine, and expect it to work.
3 Comments
$ jq . data.json
(mind the dot after the jq
).:%!jq '.'
this pretty prints the JSON in the buffer. Source: Vim: prettify JSON jq . data.json > data.json
the redirection could truncate the file before jq reads it. Using pipes with tee
has the same repercussions. askubuntu.com/questions/752174/… Instead, either create a temporary file, or use a tool like sponge
.On Ubuntu jsonlint is provided by apt:python3-demjson
Usage:
$ sudo apt install -y python3-demjson
$ jsonlint -f input.json > output.json
3 Comments
jq
, the Python json
module) require completely conformant JSON for them to work.python3-demjson
by now. The command installed with this package is still called "jsonlint" however.Add to vimrc:
" Format JSON data
map <C-F6> :%!python -m json.tool<CR>
And you can use the shortcut CTRL+F6 to format json data
Or just under vim's command mode:
%!python -m json.tool
1 Comment
au FileType json set equalprg=python\ -m\ json.tool
and format with =
.
jq
is great. You have jQuery like interface for JSON (it's homepage say it's sed for JSON) stedolan.github.io