Home > Net >  jq search specific string and output corresponding parent value
jq search specific string and output corresponding parent value

Time:07-11

I am stuck with a jq command, what I want is to search for a specific string SEARCH-TEXT in the JSON below and then output the parent value in key name: when it doesn't have a value LATEST. I tried different commands but this is how far I came:

https://jqplay.org/s/b3PEBBq8r6G

Expecting result: NOT-LATEST

Can somebody help me out with this one?

{
  "count": 685,
  "next": "https://hub.docker.com/v2/repositories/grafana/grafana/tags?page=2",
  "previous": null,
  "results": [
    {
      "creator": 4132603,
      "id": 190096972,
      "images": [
        {
          "architecture": "amd64",
          "features": "",
          "variant": null,
          "digest": "SEARCH-TEXT",
          "os": "linux",
          "os_features": "",
          "os_version": null,
          "size": 91402044,
          "status": "active",
          "last_pulled": "2022-07-11T00:19:44.535475Z",
          "last_pushed": "2022-06-28T14:15:09.891118Z"
        }
      ],
      "last_updated": "2022-06-28T14:15:10.331474Z",
      "last_updater": 2978857,
      "last_updater_username": "grafanaci",
      "name": "LATEST",
      "repository": 135617,
      "full_size": 91402044,
      "v2": true,
      "tag_status": "active",
      "tag_last_pulled": "2022-07-11T04:34:43.293126Z",
      "tag_last_pushed": "2022-06-28T14:15:10.331474Z"
    },
    {
      "creator": 2978857,
      "id": 148674560,
      "images": [
        {
          "architecture": "amd64",
          "features": "",
          "variant": null,
          "digest": "SEARCH-TEXT",
          "os": "linux",
          "os_features": "",
          "os_version": null,
          "size": 116299353,
          "status": "active",
          "last_pulled": "2022-07-11T06:32:32.095452Z",
          "last_pushed": "2022-07-08T23:02:18.104164Z"
        }
      ],
      "last_updated": "2022-07-08T23:02:18.806479Z",
      "last_updater": 2978857,
      "last_updater_username": "grafanaci",
      "name": "NOT-LATEST",
      "repository": 135617,
      "full_size": 116299353,
      "v2": true,
      "tag_status": "active",
      "tag_last_pulled": "2022-07-11T06:32:32.095452Z",
      "tag_last_pushed": "2022-07-08T23:02:18.806479Z"
    }
  ]
}

CodePudding user response:

Looks like you need an and operator such as

jq -r '.results[] | select(.name!="LATEST" and .images[].digest=="SEARCH-TEXT")| .name'

which returns

NOT-LATEST

Demo

  • Related