3

I try to sort json by date (field CreationTimeUTC) in iso 8601 format with jq 1.5.

I found this post, but that return : 'Cannot index boolean with string "CreationTimeUTC"'

jq '.Entities.BackupJobSessions.BackupJobSessions[] | sort_by(.CreationTimeUTC)'

Ok, no problem, I convert value with function 'fromdate' and obtain a unixtimestamp in integer format and sort itself, like this :

jq '.Entities.BackupJobSessions.BackupJobSessions[] | .CreationTimeUTC |= fromdate'

And try to sort :

jq '.Entities.BackupJobSessions.BackupJobSessions[] | .CreationTimeUTC |= fromdate | sort_by(.CreationTimeUTC)'

jq '.Entities.BackupJobSessions.BackupJobSessions[] | sort_by(.CreationTimeUTC |= fromdate)'

But same error : 'Cannot index boolean with string "CreationTimeUTC"'

My json :

{
  "Entities": {
    "BackupJobSessions": {
      "BackupJobSessions": [
      {
        "IsRetry": false,
        "JobUid": "urn:veeam:Job:60d09f44-654e-4831-ba59-500b4dcc7b51",
        "JobName": "Backup Job Name",
        "JobType": "Backup",
        "CreationTimeUTC": "2017-06-22T20:15:14Z",
        "EndTimeUTC": "2017-06-02T03:22:21Z",
        "State": "Stopped",
        "Result": "Success",
        "Progress": 100,
        "Name": "Backup Job Name@2017-06-02 03:15:09",
        "UID": "urn:veeam:BackupJobSession:cce79255-a71b-4a20-8806-fedcc832d12f",
        "Links": [
          {
            "Rel": "Up",
            "Href": "https://:9398/api/backupServers/6a777d6b-468f-46b3-89e6-ab48076ec1ee",
            "Name": "veeam-dcc-01",
            "Type": "BackupServerReference"
          },
          {
            "Rel": "Up",
            "Href": "https://10.10.10.10:9398/api/jobs/60d09f44-654e-4831-ba59-500b4dcc7b51",
            "Name": "Backup Job Name",
            "Type": "JobReference"
          },
          {
            "Rel": "Alternate",
            "Href": "https://10.10.10.10:9398/api/backupSessions/cce79255-a71b-4a20-8806-fedcc832d12f",
            "Name": "Backup Job Name@2017-06-02 03:15:09",
            "Type": "BackupJobSessionReference"
          },
          {
            "Rel": "Down",
            "Href": "https://10.10.10.10:9398/api/backupSessions/cce79255-a71b-4a20-8806-fedcc832d12f/taskSessions",
            "Type": "BackupTaskSessionReferenceList"
          },
          {
            "Rel": "Stop",
            "Href": "https://10.10.10.10:9398/api/backupSessions/cce79255-a71b-4a20-8806-fedcc832d12f?action=stop"
          }
        ],
        "Href": "https://10.10.10.10:9398/api/backupSessions/cce79255-a71b-4a20-8806-fedcc832d12f?format=Entity",
        "Type": "BackupJobSession"
      },
      {
        "IsRetry": false,
        "JobUid": "urn:veeam:Job:60d09f44-654e-4831-ba59-500b4dcc7b51",
        "JobName": "Backup Job Name",
        "JobType": "Backup",
        "CreationTimeUTC": "2017-06-02T03:15:09Z",
        "EndTimeUTC": "2017-06-23T01:36:20Z",
        "State": "Stopped",
        "Result": "Success",
        "Progress": 100,
        "Name": "Backup Job Name@2017-06-22 20:15:14",
        "UID": "urn:veeam:BackupJobSession:de2b2768-c36c-4e9e-a5c7-ff02473b8daa",
        "Links": [
          {
            "Rel": "Up",
            "Href": "https://10.10.10.10:9398/api/backupServers/6a777d6b-468f-46b3-89e6-ab48076ec1ee",
            "Name": "veeam-dcc-01",
            "Type": "BackupServerReference"
          },
          {
            "Rel": "Up",
            "Href": "https://10.10.10.10:9398/api/jobs/60d09f44-654e-4831-ba59-500b4dcc7b51",
            "Name": "Backup Job Name",
            "Type": "JobReference"
          },
          {
            "Rel": "Alternate",
            "Href": "https://10.10.10.10:9398/api/backupSessions/de2b2768-c36c-4e9e-a5c7-ff02473b8daa",
            "Name": "Backup Job Name@2017-06-22 20:15:14",
            "Type": "BackupJobSessionReference"
          },
          {
            "Rel": "Down",
            "Href": "https://10.10.10.10:9398/api/backupSessions/de2b2768-c36c-4e9e-a5c7-ff02473b8daa/taskSessions",
            "Type": "BackupTaskSessionReferenceList"
          },
          {
            "Rel": "Stop",
            "Href": "https://10.10.10.10:9398/api/backupSessions/de2b2768-c36c-4e9e-a5c7-ff02473b8daa?action=stop"
          }
        ],
        "Href": "https://10.10.10.10:9398/api/backupSessions/de2b2768-c36c-4e9e-a5c7-ff02473b8daa?format=Entity",
        "Type": "BackupJobSession"
      }
      ]
    }
  }
}

Best regards,

user5525652
  • 137
  • 1
  • 4
  • 12

1 Answers1

4

You're looking for:

jq '.Entities.BackupJobSessions.BackupJobSessions | sort_by(.CreationTimeUTC)'

Sorting seems to work as expected, though you could still go with:

jq '.Entities.BackupJobSessions.BackupJobSessions | sort_by(.CreationTimeUTC |= fromdate)'
SYN
  • 1,751
  • 8
  • 14