'Data extraction from array using jq

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}}



Solution 1:[1]

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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A.H.