使用jq在索引处获取满足过滤条件的JSON数组对象

问题描述 投票:0回答:1

我需要从aws cli获取list-action-executions命令返回的json数组中的整个对象,其中属性满足以下条件

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

awscli命令的输出具有以下格式。实际输出包含大约40个数组元素和满足上述条件的1个以上。我想将所有这些作为完整的对象。如何使用JQ做到这一点?我尝试了选择和索引选项,但未成功。

我的jq查询应如何格式化?

谢谢

{
  "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": {}
      }
    }
  ]
}
jq
1个回答
0
投票

[在这种情况下,人们倾向于使用jq的一个可能的错误是对整个JSON本身使用select()函数,这将not正常工作。正确的过滤器将是对数组进行过滤

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

[在jq-play上看到它的工作

或者如果您想用过滤后的结果更新原始数组,请执行此操作

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

关于您最初尝试的原因[[没有起作用,

.actionExecutionDetails[].actionName | select ( index("PromoteToProdApprovalGate") )
用于获取对象列表的路径表达式为

错误

。您基本上是在.actionName字符串字段中选择,当与select(index(..))一起使用时,它会返回布尔值true,从而使您的字符串针对每个匹配条件重复。
© www.soinside.com 2019 - 2024. All rights reserved.