Skip to main content
added 74 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

Assuming that you want the device names from the device_name key's value under logical rather than from the keys directly under logical (this makes it easier and also gives you a value that is a valid device name on Windows systems), and that you also want what I believe is the unit of measurement attached to the actual numbers (would make sense if there is a chance that you might have variable units in the data):

jq -r '
    ["Device name", "Free space", "Total size"],
    (
        .logical[] |
        [
            .device_name[0],
            (.free       | map(tostring) | add),
            (.total_size | map(tostring) | add)
        ]
    ) | @csv' file.json

Given the data in the question in file.json, this would generate the following CSV document (which would be trivial to import into Excel):

"Device name","Free space","Total size"
"D:\","10178084864B","21471686656B"
"C:\","18707755008B","55832473600B"

Change each of the two calls to add into join(" ") if you want to have a space between the number and the unit.

Assuming that you want the device names from the device_name key's value under logical rather than from the keys directly under logical (this makes it easier), and that you also want what I believe is the unit of measurement attached to the actual numbers (would make sense if there is a chance that you might have variable units in the data):

jq -r '
    ["Device name", "Free space", "Total size"],
    (
        .logical[] |
        [
            .device_name[0],
            (.free       | map(tostring) | add),
            (.total_size | map(tostring) | add)
        ]
    ) | @csv' file.json

Given the data in the question in file.json, this would generate the following CSV document (which would be trivial to import into Excel):

"Device name","Free space","Total size"
"D:\","10178084864B","21471686656B"
"C:\","18707755008B","55832473600B"

Change each of the two calls to add into join(" ") if you want to have a space between the number and the unit.

Assuming that you want the device names from the device_name key's value under logical rather than from the keys directly under logical (this makes it easier and also gives you a value that is a valid device name on Windows systems), and that you also want what I believe is the unit of measurement attached to the actual numbers (would make sense if there is a chance that you might have variable units in the data):

jq -r '
    ["Device name", "Free space", "Total size"],
    (
        .logical[] |
        [
            .device_name[0],
            (.free       | map(tostring) | add),
            (.total_size | map(tostring) | add)
        ]
    ) | @csv' file.json

Given the data in the question in file.json, this would generate the following CSV document (which would be trivial to import into Excel):

"Device name","Free space","Total size"
"D:\","10178084864B","21471686656B"
"C:\","18707755008B","55832473600B"

Change each of the two calls to add into join(" ") if you want to have a space between the number and the unit.

Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

Assuming that you want the device names from the device_name key's value under logical rather than from the keys directly under logical (this makes it easier), and that you also want what I believe is the unit of measurement attached to the actual numbers (would make sense if there is a chance that you might have variable units in the data):

jq -r '
    ["Device name", "Free space", "Total size"],
    (
        .logical[] |
        [
            .device_name[0],
            (.free       | map(tostring) | add),
            (.total_size | map(tostring) | add)
        ]
    ) | @csv' file.json

Given the data in the question in file.json, this would generate the following CSV document (which would be trivial to import into Excel):

"Device name","Free space","Total size"
"D:\","10178084864B","21471686656B"
"C:\","18707755008B","55832473600B"

Change each of the two calls to add into join(" ") if you want to have a space between the number and the unit.