Home > Enterprise >  Data extraction from array using jq
Data extraction from array using jq

Time:05-15

Please help me to figure that one out...

I want to get the ID where the name is - terraform-02

jq '.[] | map(select(.name == "terraform-02"))' - didn't work, to start filtering process

That is my array :

{"ssh_keys": [ { "id": ..., "public_key": "...", "name": "terraform-02", "fingerprint": "." }, { "id": ..., "public_key": "...", "name": "terraform-02", "fingerprint": "..." }, ],"meta": {"total": 2}}

CodePudding user response:

Given this input:

{
  "ssh_keys": [
    {
      "id": "id1",
      "public_key": "pk1",
      "name": "terraform-02",
      "fingerprint": "..."
    },
    {
      "id": "id2",
      "public_key": "pk2",
      "name": "terraform-02",
      "fingerprint": "..."
    }
  ],
  "meta": {
    "total": 2
  }
}

you can select the .id like this:

jq '.[][] | select(.name? == "terraform-02").id'

The output is:

"id1"
"id2"
  • Related