我尝试使用 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"
}
]
}
}
}
此致,
您正在寻找:
排序似乎按预期工作,但您仍然可以使用: