jq cookbook

Get AWS EC2 InstanceIds

Fetch a list of Amazon EC2 instances and get their InstanceID:

aws ec2 describe-instances | jq '.Reservations[].Instances[] | {InstanceId}'

Set a field in a Kubernetes List, if its parent exists

Update a value in an array element, where the array element has a specific value for a key (where kind is StatefulSet):

cat list.json | jq '(.items[] | select(.kind == "StatefulSet") | .spec.updateStrategy.type) |= "RollingUpdate"'

Another, less elegant way of doing the same (doesn’t search for the StatefulSet specifically but finds the tree location by the volumeClaimTemplates block):

cat output.json | jq '.items[].spec.volumeClaimTemplates[]?.spec.storageClassName = "myebs"'

Set node affinity on a DeploymentConfig


cat list.json | jq "(.items[] | select(.kind == \"DeploymentConfig\") | .spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms) |= [ { \"matchExpressions\": [ { \"key\": \"${AZ_KEY}\", \"operator\": \"In\", \"values\": [ \"${AZ_VALUE}\" ] } ] }]" \

Get a tls.crt file from a Secret

oc get secret my-secret -o json | jq -r '.data."tls.key"' | base64 -d - | openssl x509 -noout -text

Get Kinesis stream ARN

$ aws kinesis describe-stream --stream-name CillasTrades | jq -r '.StreamDescription.StreamARN'

Using jq in a while loop

while read -r email slug; do
    # do stuff

done < <(jq --raw-output '.workshop_users | to_entries[] | "\(.key) \(.value | .stack_slug)"' myfile.json)

Iterate over multiple JSON files and execute jq on each of them


for d in /workshops/provisioned-workshops/${WORKSHOP_NAME}/${WORKSHOP_NAME:0:3}*/; do
    jq -r '(.username + ":" + .password)' "$d/complete-config.json"