Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use jq to sort by datetime field?

[
{
  "apiVersion": "v1",
  "items": [
    {
      "apiVersion": "v1",
      "count": 603,
      "firstTimestamp": "2018-06-07T13:07:32Z",
      "involvedObject": {
        "apiVersion": "v1",
        "kind": "Pod",
        "name": "events-db8f675c6-khm4r",
        "namespace": "default",
        "resourceVersion": "2989590",
        "uid": "bd489878-6a53-11e8-9351-0e5486765cbc"
      },
      "kind": "Event",
      "lastTimestamp": "2018-06-07T16:02:52Z"
    }
  ]
}
]

How could I sort all the items (there is only one item above - but it's just an example) by the .items.lasttimestamp value, for example the value "lastTimestamp": "2018-06-07T11:59:53Z"?

like image 708
Chris Stryczynski Avatar asked Jun 07 '18 12:06

Chris Stryczynski


1 Answers

jq approach:

jq '.[].items |= sort_by(.lastTimestamp)' input.json

Reference: jq Manual (development version)

like image 95
RomanPerekhrest Avatar answered Nov 09 '22 23:11

RomanPerekhrest