Jq: Difference between revisions
Andre Castro (talk | contribs) No edit summary |
Andre Castro (talk | contribs) No edit summary |
||
Line 103: | Line 103: | ||
* https://shapeshed.com/jq-json/#how-to-find-items-in-an-array | * https://shapeshed.com/jq-json/#how-to-find-items-in-an-array | ||
* https://zerokspot.com/weblog/2013/07/18/processing-json-with-jq/ | * https://zerokspot.com/weblog/2013/07/18/processing-json-with-jq/ | ||
[[Category:Cookbook]] |
Revision as of 07:46, 7 October 2019
jq is a Command line JSON processor
install
- GNU/Linux:
apt install jq
- Mac:
brew install jq
Usage
Basic:prettyfier
jq is often used to pretty (make readable) JSON files or request in the shell terminal
For instances, the reply following GET Request to the PZI Wiki api: asking for the first 5 user, will look quite difficult to read
request:
curl -s "http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=allusers&aulimit=5"
response:
{"query-continue":{"allusers":{"aufrom":"Alef8"}},"query":{"allusers":[{"userid":"234","name":")biyibiyibiyi("},{"userid":"283","name":"0885629"},{"userid":"275","name":"0998247"},{"userid":"160","name":"10000BL"},{"userid":"15","name":"Albert Jongstra"}]}}
But with jq with get the JSON indented according to the hierarchy of the response request:
curl -s "http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=allusers&aulimit=5" | jq
response:
{
"query-continue": {
"allusers": {
"aufrom": "Alef8"
}
},
"query": {
"allusers": [
{
"userid": "234",
"name": ")biyibiyibiyi("
},
{
"userid": "283",
"name": "0885629"
},
{
"userid": "275",
"name": "0998247"
},
{
"userid": "160",
"name": "10000BL"
},
{
"userid": "15",
"name": "Albert Jongstra"
}
]
}
}
Filters
jq can be used as a command line filter , who's output can be sent to another application by using the '.'
instruction
request: Here grep is matching only the lines with "userid"
curl -s "http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=allusers&aulimit=5" | jq '.' | grep userid
response:
"userid": "234",
"userid": "283",
"userid": "275",
"userid": "160",
"userid": "15",
Filters for querying
jq Filters can be used for querying:
Using the following request if would like to receive only the value of the users names.
If we look at the structure of the response we see that each user name
is wrapped withing the following structure
"query": {
"allusers": [
{
"userid": "234",
"name": ")biyibiyibiyi("
}
]
}
Which we could handle in jq by doing the following steps:
jq '.query'
the value of key "query"jq '.query.allusers'
the value of key "allusers", within the value of key "query", which is a listjq '.query.allusers[]'
all the items of list "allusers"jq '.query.allusers[0]'
only the 1st item of listjq '.query.allusers[0,2]'
items 0, 2 of listjq '.query.allusers[1:]'
all items but firstjq '.query.allusers[:-1]'
all items but last
jq '.query.allusers[].name'
the value of key "name" for all items of list "allusers"