jq:从子数组中获取值并显示在父数组上

jq:从子数组中获取值并显示在父数组上

我尝试获取这些价值:

来自此 json 的“VmDisplayName、CreationTimeUTC、EndTimeUTC、Reason、Result、State、TotalSize、BackupServerReference、BackupJobSessionReference”:

{
  "JobSessionUid": "urn:veeam:BackupJobSession:fe8a7b44-5d5d-4767-e5f4-db0dba854c59",
  "CreationTimeUTC": "2017-11-14T21:02:54Z",
  "EndTimeUTC": "2017-11-14T21:03:30Z",
  "State": "Completed",
  "Result": "Success",
  "Reason": "",
  "TotalSize": 599785472,
  "VmUid": "urn:VMware:Vm:11e45620-a0dc-1278-6d13-11a58ce70564.vm-1128",
  "VmDisplayName": "VMName",
  "Name": "VMName@2017-11-14 21:02:54",
  "UID": "urn:veeam:BackupTaskSession:540c9985-52ab-ab01-a453-e0126edf8716",
  "Links": [
    {
      "Rel": "Up",
      "Href": "https://10.255.147.125:9398/api/backupServers/e435adc7-63f9-4115-be34-0a647e8faa1e",
      "Name": "veeam-dcc-02",
      "Type": "BackupServerReference"
    },
    {
      "Rel": "Up",
      "Href": "https://10.255.147.125:9398/api/backupSessions/fe8a7b44-5d5d-4767-e5f4-db0dba854c59",
      "Name": "Backup Name@2017-11-14 21:00:02",
      "Type": "BackupJobSessionReference"
    },
    {
      "Rel": "Alternate",
      "Href": "https://10.255.147.125:9398/api/backupTaskSessions/540c9985-52ab-ab01-a453-e0126edf8716",
      "Name": "VMName@2017-11-14 21:02:54",
      "Type": "BackupTaskSessionReference"
    },
    {
      "Rel": "Related",
      "Href": "https://10.255.147.125:9398/api/vmRestorePoints/6f27c79f-1d5c-4cf9-b5f8-43143447bc00?format=Entity",
      "Name": "VMName@2017-11-14 21:03:03",
      "Type": "VmRestorePoint"
    }
  ],
  "Href": "https://10.255.147.125:9398/api/backupTaskSessions/540c9985-52ab-ab01-a453-e0126edf8716?format=Entity",
  "Type": "BackupTaskSession"
}

目标是使用 jq 显示根 json 中的所有元素,如下所示:

{
  "JobSessionUid": "urn:veeam:BackupJobSession:fe8a7b44-5d5d-4767-e5f4-db0dba854c59",
  "CreationTimeUTC": "2017-11-14T21:02:54Z",
  "EndTimeUTC": "2017-11-14T21:03:30Z",
  "State": "Completed",
  "Result": "Success",
  "Reason": "",
  "TotalSize": 599785472,
  "VmUid": "urn:VMware:Vm:11e45620-a0dc-1278-6d13-11a58ce70564.vm-1128",
  "VmDisplayName": "VMName",
  "Name": "VMName@2017-11-14 21:02:54",
  "UID": "urn:veeam:BackupTaskSession:540c9985-52ab-ab01-a453-e0126edf8716",
  "BackupServerReference": "veeam-dcc-02",
  "BackupJobSessionReference": "Backup Name@2017-11-14 21:00:02",
}

我尝试了几个命令但没有成功:

jq '{VmDisplayName,CreationTimeUTC,EndTimeUTC,Reason,Result,State,TotalSize} as $root | (.Links[] | select((.Type == "BackupServerReference")  or (.Type == "BackupJobSessionReference"))) | . as $ref | $root + $ref'

jq '{VmDisplayName,CreationTimeUTC,EndTimeUTC,Reason,Result,State,TotalSize} as $root | (.Links[] | + {"BackupServerReference": select(.Type == "BackupServerReference")})  | . as $ref | $root + $ref'

你能帮助我吗 ?

此致,

答案1

这个完整的查询应该产生您想要的输出:

jq '[del(.Links), (.Links[] | { (.Type): .Name })] | add'

细分如下:

del(.Links)除链接之外的所有元素。

(.Links[] | { (.Type): .Name })来自每个孩子的物品链接包含类型作为关键和姓名作为价值。

[...] | add将所有对象合并到一个数组中,然后将它们添加在一起作为一个对象。

输出:

{
  "JobSessionUid": "urn:veeam:BackupJobSession:fe8a7b44-5d5d-4767-e5f4-db0dba854c59",
  "CreationTimeUTC": "2017-11-14T21:02:54Z",
  "EndTimeUTC": "2017-11-14T21:03:30Z",
  "State": "Completed",
  "Result": "Success",
  "Reason": "",
  "TotalSize": 599785472,
  "VmUid": "urn:VMware:Vm:11e45620-a0dc-1278-6d13-11a58ce70564.vm-1128",
  "VmDisplayName": "VMName",
  "Name": "VMName@2017-11-14 21:02:54",
  "UID": "urn:veeam:BackupTaskSession:540c9985-52ab-ab01-a453-e0126edf8716",
  "Href": "https://10.255.147.125:9398/api/backupTaskSessions/540c9985-52ab-ab01-a453-e0126edf8716?format=Entity",
  "Type": "BackupTaskSession",
  "BackupServerReference": "veeam-dcc-02",
  "BackupJobSessionReference": "Backup Name@2017-11-14 21:00:02",
  "BackupTaskSessionReference": "VMName@2017-11-14 21:02:54",
  "VmRestorePoint": "VMName@2017-11-14 21:03:03"
}

相关内容