user3893836
user3893836

Reputation: 85

JSON parse with jq and filter

I am learning jq. I have content as below:

{
   "amphorae_links":[

   ],
   "amphorae":[
      {
         "status":"BOOTING",
         "loadbalancer_id":null,
         "created_at":"2020-06-23T08:56:56",
         "vrrp_id":null,
         "id":"6d66935e-6d39-40c9-bb0d-dd6a734dc77b"
      },
      {
         "status":"ALLOCATED",
         "loadbalancer_id":"79970c9a-b0ba-4cde-a7e6-16b61641a7b8",
         "created_at":"2020-06-25T06:41:56",
         "vrrp_id":1,
         "id":"872c08ee-9b21-4b26-9550-c2ffb4a1ad59"
      }
   ]
}

I want to have an output like

"ALLOCATED=872c08ee-9b21-4b26-9550-c2ffb4a1ad59,79970c9a-b0ba-4cde-a7e6-16b61641a7b8"

I tried to use below, but I don't know how to remove the line with status as "BOOTING".

.amphorae[] | "\(.status)=\(.id),\(.loadbalancer_id)"

Thanks anyone for the helping.

Upvotes: 0

Views: 74

Answers (1)

Adim
Adim

Reputation: 1776

You have a couple of options:

  1. You can specify the second index of the amphorae array, that is if you are sure the "ALLOCATED" part will always be the second index of the amphorae array
    .amphorae[1] | "\(.status)=\(.id),\(.loadbalancer_id)"
  1. This is a little bit more complex, but will always ensure you get only the element of the amphorae array where the status key is 'ALLOCATED'
.amphorae | map(select(.status == "ALLOCATED"))[] | "\(.status)=\(.id),\(.loadbalancer_id)"

Upvotes: 2

Related Questions