Usage of jq the JSON processor
jq
is a powerful and easy-to-use JSON processor on command line. Here are 3 examples of how I use it in my daily work.
1. Pretty-print the output of curl
When you call a REST API, which returns JSON as response, you can use it to make the reponse more readable like this:
curl https://api.example.com/... | jq
2. Use in Vim
I have the following lines in my .vimrc
file:
" user jq to format JSON file
nmap =j :%!jq '.'<CR>
When I eidt JSON file with Vim, I can just hit =j
, it will be formatted instantly.
3. Extract fields in JSON
jq
can also extract certain fields in JSON. Here is an example to get the names of all my repos on Github:
curl https://api.github.com/users/feici02/repos | jq '.[].name'
For more infomation, please refer to man jq
.