adbdkb
adbdkb

Reputation: 2161

Get JSON array object with an attribute having latest timestamp

I was able to get the array elements satisfying a set of filters from output of list-action-executions in this question Of the returned objects, how can I select the object that has latest value for lastUpdateTime attribute

This is the format of input that will be available from the output of the linked question

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

Thanks

Upvotes: 0

Views: 160

Answers (1)

Inian
Inian

Reputation: 85663

You can use max_by and provide a path expression, using which you can sort the array of objects.

.actionExecutionDetails
| max_by(.startTime)

This is functionally equivalent to doing a sort by the field and get the last element in the array. By default sort() function does an ascending sort of the values and does not provide an argument to do a descending sort.

.actionExecutionDetails
| sort_by(.startTime)
| last

Upvotes: 1

Related Questions