Jq

From Halfface
Jump to navigation Jump to search

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

Select values in a chain.

oc get machinesets -n openshift-machine-api -o json | jq -r '.items[] | select(.spec.template.metadata.labels."machine.openshift.io/cluster-api-machine-role" == "worker")| .metadata.name'

select with regex against string in output

openstack command list -f json | jq -r '.[].Commands[]|select (match("list$"))'

select where value is not null

select(.spec.template.metadata.labels."machine.openshift.io/cluster-api-machine-role" == "worker")

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

convert yaml too json

dnf install python3-pyyaml
oc get node -o yaml | python -c 'import sys, yaml, json; json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=4)' | jq .

jwt

jq -R 'split(".") | .[0],.[1] | @base64d | fromjson' <file>

join results

oc get endpoints -o json -A | jq -r '.items[].subsets[] | [.addresses[0].ip,.ports[0].port]|join(":")'
oc get clusterversion -o json | jq -r '.items[0].status.history[] |  [.version, .startedTime, .completionTime] | join(" ")'