Json
jq :從子數組中獲取值並顯示在父數組上
我試圖獲得這些價值:
來自此 json 的“VmDisplayName、CreationTimeUTC、EndTimeUTC、原因、結果、狀態、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'
你能幫助我嗎 ?
最好的祝福,
這個完整的查詢應該產生你想要的輸出:
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" }