使用jq在满足过滤条件的索引处获取JSON数组对象 [英] Get JSON array object at index satisfying a filter condition using jq

查看:84
本文介绍了使用jq在满足过滤条件的索引处获取JSON数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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",

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

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.

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

How should my jq query be formatted?

谢谢

{
  "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": {}
      }
    }
  ]
}

推荐答案

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

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")'

看到它可以在 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") )

获取对象列表的路径表达式为错误.您基本上是在.actionName字符串字段中选择的,当与select(index(..))一起使用时,它会返回布尔值true,从而导致您的字符串针对每个匹配条件重复.

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.

这篇关于使用jq在满足过滤条件的索引处获取JSON数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆