Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort by date with jmespath

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?

like image 926
Mio Avatar asked Feb 23 '17 11:02

Mio


People also ask

What is JMESPath expression?

JMESPath (JSON Matching Expression paths) is a query language for search JSON documents. It allows you to declaratively extract elements from a JSON document.

What is the use of JMESPath?

JMESPath (pronounced “james path”) allows you to declaratively specify how to extract elements from a JSON document.

Does JQ use JMESPath?

jq is typically used for the former, JMESPath for the latter. There's no reason why the remote service couldn't accept a jq filter, or that you couldn't use a JMESPath-based executable. Thanks for your answer!


2 Answers

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]"
like image 62
Mio Avatar answered Sep 18 '22 17:09

Mio


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

--query "reverse(sort_by([?starts_with(name, 'abc')], &properties.lastModified))[:1]"
like image 37
Ramanujam Allam Avatar answered Sep 20 '22 17:09

Ramanujam Allam