Jq: Difference between revisions

From Halfface
Jump to navigation Jump to search
Line 1: Line 1:
=jq=
==how many stores are actually in there:==
$ cat file.json | jq 'length'
1134
==retrieves the “name” field of each element of the input array.==
cat file.json | jq '.[] | .name'
"Graz"
"Baden"
"Zürich"
...
==Give us first store.==
jq '.[0]'
==Select specific fields==
jq '.[] | {eta: .eta, ticketID: .ticketID}'
==Select specific files shortended.==
jq '.[] | {eta, ticketID}'
==url encode a string.==
jq -sRr @uri <<< "${IPRADAR_SERVICE}" | sed 's/%0A$//g'
==Pick extra_vars under results in raw format.==
jq -r '.results[]|.extra_vars'
==Select array name based on content.==
jq -r '.[]|select(.name=="the name you want to print")'
==Display keys==
jq 'keys'
==Select specific key==
  jq -r '. | {all}'
==select key based on content==
jq 'map_values(select(.group_declare == "true"))' /tmp/tmp
=create list of all keys in array.=
=create list of all keys in array.=
  curl -sk -H "Authorization: Bearer $(oc whoami -t)" https://$(oc get routes -n openshift-monitoring thanos-querier -o jsonpath='{.status.ingress[0].host}')/api/v1/metadata | jq .
  curl -sk -H "Authorization: Bearer $(oc whoami -t)" https://$(oc get routes -n openshift-monitoring thanos-querier -o jsonpath='{.status.ingress[0].host}')/api/v1/metadata | jq .

Revision as of 10:45, 25 May 2023

jq

how many stores are actually in there:

$ cat file.json | jq 'length'
1134

retrieves the “name” field of each element of the input array.

cat file.json | jq '.[] | .name'
"Graz"
"Baden"
"Zürich"
...

Give us first store.

jq '.[0]'

Select specific fields

jq '.[] | {eta: .eta, ticketID: .ticketID}'

Select specific files shortended.

jq '.[] | {eta, ticketID}'

url encode a string.

jq -sRr @uri <<< "${IPRADAR_SERVICE}" | sed 's/%0A$//g'

Pick extra_vars under results in raw format.

jq -r '.results[]|.extra_vars'

Select array name based on content.

jq -r '.[]|select(.name=="the name you want to print")'

Display keys

jq 'keys'

Select specific key

 jq -r '. | {all}'

select key based on content

jq 'map_values(select(.group_declare == "true"))' /tmp/tmp

create list of all keys in array.

curl -sk -H "Authorization: Bearer $(oc whoami -t)" https://$(oc get routes -n openshift-monitoring thanos-querier -o jsonpath='{.status.ingress[0].host}')/api/v1/metadata | jq .
echo '{
  "data": {
    "aggregator_openapi_v2_regeneration_count": [
      {
        "unit": ""
       }
    ],
    "alertmanager_alerts": [
      {
        "unit": ""
      }
    ]
  }
}' | jq -r '.data | keys | flatten[]'
aggregator_openapi_v2_regeneration_count
alertmanager_alerts