jq 按日期排序(iso 8601)

jq 按日期排序(iso 8601)

我尝试使用 jq 1.5 按日期(字段 CreationTimeUTC)以 iso 8601 格式对 json 进行排序。

我发现这个帖子,但返回:'无法使用字符串“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"
      }
      ]
    }
  }
}

此致,

答案1

您正在寻找:

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

排序似乎按预期工作,但您仍然可以这样做:

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

相关内容