Jq: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 98: Line 98:
* <code>jq '.query.allusers[].name'</code> '''the value of key "name"''' for all items of list "allusers"
* <code>jq '.query.allusers[].name'</code> '''the value of key "name"''' for all items of list "allusers"


request:
<source lang="bash">curl -s "http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=allusers&aulimit=5"  | jq '.query.allusers[].name'</source>
response:
<source lang="bash">
")biyibiyibiyi("
"0885629"
"0998247"
"10000BL"
"Albert Jongstra"
</source>


==References==
==References==

Revision as of 08:48, 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 list
  • jq '.query.allusers[]' all the items of list "allusers"
    • jq '.query.allusers[0]' only the 1st item of list
    • jq '.query.allusers[0,2]' items 0, 2 of list
    • jq '.query.allusers[1:]' all items but first
    • jq '.query.allusers[:-1]' all items but last
  • jq '.query.allusers[].name' the value of key "name" for all items of list "allusers"

request:

curl -s "http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=allusers&aulimit=5"  | jq '.query.allusers[].name'

response:

")biyibiyibiyi("
"0885629"
"0998247"
"10000BL"
"Albert Jongstra"

References