User Tools

Site Tools


virtualization:cloud:aws:jq

This is an old revision of the document!


JQ stands for 'json query'

  • Objects (dictionaries)
  • Arrays (lists)
  • strings
jq . # the dots means 'applies for all objects below'
aws ec2 describe-regions  | jq -r "[[.Regions[].RegionName]]"
aws ec2 describe-vpcs | jq -r "[[.Reservations[].Instances[].InstanceId]]"  # extract all instances id (down 3 levels)
aws ec2 describe-vpcs | jq -r '.Vpcs | length'   # COUNT number of VPCs
aws ec2 describe-vpcs --region eu-west-1 | jq -r '.Vpcs | length'    # For a DIFFERENT Region

Lists the public IPs (querying the OCI DNS):

oci dns record zone get --zone-name-or-id oci.grapeshot.co.uk | jq -r '.[].items[] | .domain + " " + .rdata'
oci dns record zone get --zone-name-or-id lhr.oci.grapeshot.co.uk | jq -r '.[].items[] | .domain + " " + .rdata'
oci compute instance get --instance-id ocid1.instance.oc1.uk-london-1.anwgiljrgghpcsic6lrm3u5zqh7vwqqvsv44ewyjybis3lzhhb7bqmizelrq | jq -r '.data | .["display-name"]' # how to extract the keys that OCI annoying put hyphens in rather than underscores
oci network vcn list --region=eu-frankfurt-1 --compartment-id=$cs | jq -r '.data[] | .["compartment-id"]'

Folsom:

cat /home/jaime/Downloads/response.json | jq  -r '.response[].attributes | .display_name ' > /home/jaime/Downloads/response1.json
  • response is 1st level, and has many 'string' elements under it
  • which are attribute blocks
  • | now stuff inside the 'block'
oci compute instance list --compartment-id=$cb | jq -r '.data[]|select (."display-name"=="pepe37.lhr.oci.grapeshot.co.uk")'
oci compute instance list --compartment-id=$cb | jq -r '.data[]|select (."display-name" | contains("cc0"))'  # like regex 'contains'

This filters with id but extracts the pair, not the key https://til.hashrocket.com/posts/uv0bjiokwk-use-jq-to-filter-objects-list-with-regex

virtualization/cloud/aws/jq.1614850658.txt.gz · Last modified: (external edit)