0

I have a Json file like below,

{
  "tasks": [
    {
      "id": "nightly_1652299200",
      "repo": "tx/tx5",
      "branch": "dev",
      "type": "HealthCheck",
      "started_on": "2022-05-11 23:00:00 +0300 EEST",
      "state": "Running",
      "top_sha": "10143120c12cd506fca5a466a7c6b27e7cb35b18",
      "tested_shas": null,
      "self_url": "tx/tx5/10143120c12cd506fca5a466a7c6b27e7cb35b18",
      "is_public": true
    },
    {
      "id": "1652298808",
      "repo": "tx/tx3d",
      "branch": "6.3",
      "type": "Integration",
      "started_on": "2022-05-11 22:53:28 +0300 EEST",
      "state": "Passed",
      "top_sha": "40ca8db14a8ddd082270a81e895bf01048949ec4",
      "tested_shas": null,
      "self_url": "tx/tx3d/40ca8db14a8ddd082270a81e895bf01048949ec4",
      "is_public": true
    },
    {
      "id": "1652298252",
      "repo": "tx/qtmultimedia",
      "branch": "dev",
      "type": "Integration",
      "started_on": "2022-05-11 22:44:12 +0300 EEST",
      "state": "Failed",
      "top_sha": "d7dd593b6395a33862742a04a23d88b970f7b914",
      "tested_shas": null,
      "self_url": "qt/qtmultimedia/d7dd593b6395a33862742a04a23d88b970f7b914",
      "is_public": true
    }
}

I need to find the ID of each and every Passed tasks ("state": "Passed") using a bash script. what will be the best approach for this.

anXler
  • 175
  • 1
  • 1
  • 12
  • Note that it's good practice to show what you tried and how it failed; showing exactly where you got stuck helps to disambiguate a question so it doesn't get marked duplicate of the hundreds of other "how do I extract a field from a JSON file in bash?" questions. – Charles Duffy May 11 '22 at 20:36

1 Answers1

1

Using jq:

jq -r '.tasks | .[] | select (.state == "Passed") | .id' file.json
Fravadona
  • 13,917
  • 1
  • 23
  • 35