adbdkb
adbdkb

Reputation: 2161

Get JSON array object at index satisfying a filter condition using jq

I need to get the entire object(s) in json array returned by list-action-executions command from aws cli, where the attributes satisfy the condition below

"stageName": "DeployStage",
"actionName": "PromoteToProdApprovalGate",

The output from the awscli command is of the format. The actual output contains about 40 array elements and more than 1 satisfying the above conditions. I would like to get all of them as full objects. How can I do it with JQ? I tried the select and index options, but was not successful.

How should my jq query be formatted?

Thank you

{
  "actionExecutionDetails": [
    {
      "pipelineExecutionId": "pipeline-exec-id-1",
      "actionExecutionId": "action-exec-id-1",
      "pipelineVersion": 2,
      "stageName": "DeployStage",
      "actionName": "PromoteToProdApprovalGate",
      "startTime": "2020-06-01T22:11:53-04:00",
      "lastUpdateTime": "2020-06-01T22:11:53-04:00",
      "status": "InProgress",
      "input": {
        "actionTypeId": {
          "category": "Approval",
          "owner": "AWS",
          "provider": "Manual",
          "version": "1"
        },
        "configuration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=#{SourceBuildVariables.BB_COMMIT_ID}",
          "ExternalEntityLink": "#{SourceBuildVariables.BB_URL}",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "resolvedConfiguration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=bb-commit-id-1",
          "ExternalEntityLink": "url/bb-commit-id",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "region": "us-east-1",
        "inputArtifacts": []
      },
      "output": {
        "outputArtifacts": [],
        "outputVariables": {}
      }
    },
    {
      "pipelineExecutionId": "pipeline-exec-id-2",
      "actionExecutionId": "action-exec-id-2",
      "pipelineVersion": 2,
      "stageName": "DeployStage",
      "actionName": "ImageEnvironmentTag",
      "startTime": "2020-06-01T22:09:45.833000-04:00",
      "lastUpdateTime": "2020-06-01T22:11:52.689000-04:00",
      "status": "Succeeded",
      "input": {
        "actionTypeId": {
          "category": "Build",
          "owner": "AWS",
          "provider": "CodeBuild",
          "version": "1"
        },
        "configuration": {
          "EnvironmentVariables": "[\n  { \"name\": \"LAST_ENV\", \"value\": \"qa\" },\n  { \"name\": \"BB_COMMIT_ID\", \"value\": \"#{SourceBuildVariables.BB_COMMIT_ID}\" }\n]\n",
          "ProjectName": "project-name-environement-tag"
        },
        "resolvedConfiguration": {
          "EnvironmentVariables": "[\n  { \"name\": \"LAST_ENV\", \"value\": \"qa\" },\n  { \"name\": \"BB_COMMIT_ID\", \"value\": \"bb-commit-id-1\" }\n]\n",
          "ProjectName": "projectName"
        },
        "region": "us-east-1",
        "inputArtifacts": [
          {
            "name": "PipelineArtifacts",
            "s3location": {
              "bucket": "bucket",
              "key": "key"
            }
          }
        ]
      },
      "output": {
        "outputArtifacts": [],
        "executionResult" : {
          "externalExecutionId": "externalExecutionId",
          "externalExecutionUrl": "https://console.aws.amazon.com/codebuild/home?region=us-east-1#/builds/"
        },
        "outputVariables": {}
      }
    },
    {
      "pipelineExecutionId": "",
      "actionExecutionId": "",
      "pipelineVersion": 2,
      "stageName": "DeployStage",
      "actionName": "PromoteToProdApprovalGate",
      "startTime": "2020-06-01T22:11:53-04:00",
      "lastUpdateTime": "2020-06-01T22:11:53-04:00",
      "status": "InProgress",
      "input": {
        "actionTypeId": {
          "category": "Approval",
          "owner": "AWS",
          "provider": "Manual",
          "version": "1"
        },
        "configuration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=#{SourceBuildVariables.BB_COMMIT_ID}",
          "ExternalEntityLink": "#{SourceBuildVariables.BB_URL}",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "resolvedConfiguration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=xxx1",
          "ExternalEntityLink": "http://",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "region": "us-east-1",
        "inputArtifacts": []
      },
      "output": {
        "outputArtifacts": [],
        "outputVariables": {}
      }
    },
    {
      "pipelineExecutionId": "",
      "actionExecutionId": "",
      "pipelineVersion": 1,
      "stageName": "DeployStage",
      "actionName": "PromoteToProdApprovalGate",
      "startTime": "2020-03-31T23:29:14.479000-04:00",
      "lastUpdateTime": "2020-04-03T19:04:51.646000-04:00",
      "status": "Succeeded",
      "input": {
        "actionTypeId": {
          "category": "Approval",
          "owner": "AWS",
          "provider": "Manual",
          "version": "1"
        },
        "configuration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=#{SourceBuildVariables.BB_COMMIT_ID}",
          "ExternalEntityLink": "#{SourceBuildVariables.BB_URL}",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "resolvedConfiguration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=xxx2",
          "ExternalEntityLink": "http://",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "region": "us-east-1",
        "inputArtifacts": []
      },
      "output": {
        "outputArtifacts": [],
        "executionResult": {
          "externalExecutionId": ",
          "externalExecutionSummary": "Approved by arn:aws:sts:"
        },
        "outputVariables": {}
      }
    },
    {
      "pipelineExecutionId": "",
      "actionExecutionId": "",
      "pipelineVersion": 1,
      "stageName": "DeployStage",
      "actionName": "PromoteToProdApprovalGate",
      "startTime": "2020-03-18T21:10:25.541000-04:00",
      "lastUpdateTime": "2020-03-25T21:10:25.965000-04:00",
      "status": "Failed",
      "input": {
        "actionTypeId": {
          "category": "Approval",
          "owner": "AWS",
          "provider": "Manual",
          "version": "1"
        },
        "configuration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=#{SourceBuildVariables.BB_COMMIT_ID}",
          "ExternalEntityLink": "#{SourceBuildVariables.BB_URL}",
          "NotificationArn": "arn:aws:sns:us-east-1"
        },
        "resolvedConfiguration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=xxx3",
          "ExternalEntityLink": "http://",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "region": "us-east-1",
        "inputArtifacts": []
      },
      "output": {
        "outputArtifacts": [],
        "executionResult": {
          "externalExecutionId": ""
        },
        "outputVariables": {}
      }
    },
    {
      "pipelineExecutionId": "",
      "actionExecutionId": "",
      "pipelineVersion": 1,
      "stageName": "DeployStage",
      "actionName": "PromoteToProdApprovalGate",
      "startTime": "2020-03-09T19:23:43.637000-04:00",
      "lastUpdateTime": "2020-03-10T14:48:30.069000-04:00",
      "status": "Failed",
      "input": {
        "actionTypeId": {
          "category": "Approval",
          "owner": "AWS",
          "provider": "Manual",
          "version": "1"
        },
        "configuration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=#{SourceBuildVariables.BB_COMMIT_ID}",
          "ExternalEntityLink": "#{SourceBuildVariables.BB_URL}",
          "NotificationArn": "arn:aws:sns:us-east-1"
        },
        "resolvedConfiguration": {
          "CustomData": "Deploy Service to Prod Approval Required for CommitID=xxx4",
          "ExternalEntityLink": "http://",
          "NotificationArn": "arn:aws:sns:us-east-1:"
        },
        "region": "us-east-1",
        "inputArtifacts": []
      },
      "output": {
        "outputArtifacts": [],
        "executionResult": {
          "externalExecutionId": "",
          "externalExecutionSummary": ""
        },
        "outputVariables": {}
      }
    }
  ]
}

Upvotes: 0

Views: 662

Answers (1)

Inian
Inian

Reputation: 85693

One likely mistake people tend to do with jq on such situations is to use the select() function to the entire JSON itself which would not work as expected. The right filter would be to filter on the array

.actionExecutionDetails[]
| select(.stageName == "DeployStage" and .actionName == "PromoteToProdApprovalGate")'

See it working on jq-play

Or if you want the original array updated with the filtered result, do this

.actionExecutionDetails |= 
map(select(.stageName == "DeployStage" and .actionName == "PromoteToProdApprovalGate"))

As for why your original attempt didn't work,

.actionExecutionDetails[].actionName 
| select ( index("PromoteToProdApprovalGate") )

the path expression to get the objects list is wrong. You are basically selecting on the .actionName string field which when used with select(index(..)) returns a bool value true, causing your string to repeat for each match condition.

Upvotes: 1

Related Questions