1

I have the following json file which this value

{
  "data": [
      {
          "id": 60573936,
          "timeCreated": 1615458564202,
          "timeUpdated": 1615458564202,
          "name": "name1",
          "desiredStatus": "UNMANAGED",
          "defaultStatus": "STARTED",
          "targets": [
              {
                  "id": 2520949,
                  "lastReportedStatus": "STOPPED"
              }, {
                  "id": 702881,
                  "lastReportedStatus": "STARTED"
              }
          ]
      }, {
          "id": 60574370,
          "timeCreated": 1615458812565,
          "timeUpdated": 1615458812565,
          "name": "name2",
          "desiredStatus": "UNMANAGED",
          "defaultStatus": "STARTED",
          "targets": [
              {
                  "id": 2520949,
                  "lastReportedStatus": "STARTED"
              }, {
                  "id": 702881,
                  "lastReportedStatus": "STOPPED"
              }
          ]
      }, {
          "id": 60574329,
          "timeCreated": 1615458775053,
          "timeUpdated": 1615458775053,
          "name": "name3",
          "desiredStatus": "UNMANAGED",
          "defaultStatus": "STARTED",
          "targets": [
              {
                  "id": 2520949,
                  "lastReportedStatus": "STOPPED"
              }, {
                  "id": 702881,
                  "lastReportedStatus": "STARTED"
              }
          ]
      }
    ]
  }

I would like to extract the "name" value only if "id"="2520949" and "lastReportedStatus"="STOPPED" using jq. In my example I would like to get "name1" and "name3". I tried using select feature but I'm not able to satisfy the "and" condition between "id" and "lastReportedStatus" key. What is the correct code with jq ?

0

1 Answer 1

7
jq '.data[] 
    | select(.targets[] | .lastReportedStatus=="STOPPED" and .id==2520949)
    | .name' file.json
Sign up to request clarification or add additional context in comments.

1 Comment

"Thanks" on StackOverflow = Upvote / Accept :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.