Sort by date with jmespath
Asked Answered
C

2

19

With a json output like :

{
   "Functions":[
      {
         "CodeSha256":"7NBvXXacp9x3aK3cKaI=",
         "FunctionName":"function_1",
         "FunctionArn":"arn:aws:lambda:eu-west-1:1111:function:function_1",
         "LastModified":"2015-02-09T11:35:31.084+0000"
      },
      {
         "CodeSha256":"7NBvXXacKaI=",
         "FunctionName":"function_3",
         "FunctionArn":"arn:aws:lambda:eu-west-1:1111:function:function_3",
         "LastModified":"2015-03-09T11:35:31.084+0000"
      },
      {
         "CodeSha256":"7NBvXXacaK3cKaI=",
         "FunctionName":"function_2",
         "FunctionArn":"arn:aws:lambda:eu-west-1:1111:function:function_2",
         "LastModified":"2015-02-11T11:35:31.084+0000"
      }
   ]
}

How can I return the two most recent Functions sorted by LastModified?

Cima answered 23/2, 2017 at 11:13 Comment(0)
C
32

You need to use reverse and sort_by first. Then add [:2] for only two record :

aws lambda list-functions --query "reverse(sort_by(Functions, &LastModified))[:2]"
Cima answered 24/2, 2017 at 13:40 Comment(2)
It doesn't work in all case because jmespath consider this: "215-02-09T11:35:31.084+0000" > "2015-02-09T11:35:31.084+0000"return true so you need to have date with the same amount of digit to apply the method above.Gaffney
@Gaffney Yep, coming to this late, but won't ISO dates all have the same number of characters, hence, for this question, the answer is correct? The crux of what you appear to be saying is that jmespath sorts by string or number only. It doesn't sort by date, as such but with ISO date format string sorting works pretty well.Saberio
A
9

If you need top 1 last modified, whose name starts with 'abc'

--query "reverse(sort_by([?starts_with(name, 'abc')], &properties.lastModified))[:1]"
Ai answered 11/5, 2018 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.