Date

jq 按日期排序 (iso 8601)

  • November 19, 2019

我嘗試使用 jq 1.5 以 iso 8601 格式按日期(欄位 CreationTimeUTC)對 json 進行排序。

我找到了這篇文章,但返回:‘Cannot index boolean with string “CreationTimeUTC”’

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

好的,沒問題,我用函式’fromdate’轉換值並獲得整數格式的unixtimestamp並自行排序,如下所示:

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

並嘗試排序:

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

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

但同樣的錯誤:‘不能用字元串“CreationTimeUTC”索引布爾值’

我的 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"
     }
     ]
   }
 }
}

最好的祝福,

您正在尋找:

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

排序似乎按預期工作,但您仍然可以使用:

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

引用自:https://serverfault.com/questions/992406