2

I try to get theses value :

"VmDisplayName, CreationTimeUTC, EndTimeUTC, Reason, Result, State, TotalSize, BackupServerReference, BackupJobSessionReference" from this 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",
  "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"
}

The objective is to display all elements in root json with jq, like this :

{
  "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",
}

I try several commands without success :

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'

Can you help me ?

Best regards,

2

This full query should produce the output you want:

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

Broken down:

del(.Links) All elements except for Links.

(.Links[] | { (.Type): .Name }) Objects from every child in Links containing Type as the key and Name as the Value.

[...] | add Combine all objects in an array and add them together as one single object.

Outputs:

{
  "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"
}
2
  • That works, thank you very much! – user5525652 Nov 15 '19 at 14:31
  • Nice one ... I learned a lot figuring out how this worked. I ended up with del(.Links) + ([.Links[] | { (.Type): .Name }] | add) which I think is a bit cleaner but that could be personal preference. – Bean Taxi Apr 15 '20 at 0:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.