0

I need to fetch below objects from json which I am getting via API. I am trying with jq which is available on ubuntu 16.04. I am trying to get data in this format device_name C:| free 18707755008.0 total_size 55832473600.0

Any suggest how should i approach on this ? The goal is get above 3 object in excel.

{
    "logical": {
        "D:|": {
            "device_name": [
                "D:\\"
            ],
            "used_percent": [
                52.6,
                "%"
            ],
            "fstype": "NTFS",
            "free": [
                10178084864.0,
                "B"
            ],
            "total_size": [
                21471686656.0,
                "B"
            ],
            "used": [
                11293601792.0,
                "B"
            ],
            "opts": "rw,fixed"
        },
        "C:|": {
            "device_name": [
                "C:\\"
            ],
            "used_percent": [
                66.5,
                "%"
            ],
            "fstype": "NTFS",
            "free": [
                18707755008.0,
                "B"
            ],
            "total_size": [
                55832473600.0,
                "B"
            ],
            "used": [
                37124718592.0,
                "B"
            ],
            "opts": "rw,fixed"
        }
    }
}
2

The following outputs 2 lines with a string format seen on the last line you can easily change:

jq -M -r '.[] | keys[] as $var |
 [.[$var].free[0], .[$var].total_size[0]] as [$f,$t] |
 "device_name \($var) free \($f) total_size \($t)" '

output:

device_name C:| free 18707755008 total_size 55832473600
device_name D:| free 10178084864 total_size 21471686656
  • I get error jq -M -r '.[] | keys[] as $var | [.[$var].free[0], .[$var].total_size[0]] as [$f,$t] | "device_name \($var) free \($f) total_size \($t)" ' disk.json error: syntax error, unexpected '[', expecting '$' .[] | keys[] as $var | [.[$var].free[0], .[$var].total_size[0]] as [$f,$t] | "device_name ($var) free ($f) total_size ($t)" 1 compile error – user138530 Oct 13 '18 at 17:48
  • I am using jq 1.5. What does jq --version say? – meuh Oct 13 '18 at 18:01
  • jq -V jq-1.4-1-e73951f for this it doesn't works jq -V jq-1.5-1-a5b5cbe for this version it works – user138530 Oct 15 '18 at 3:13
0

Use this :

jq '.logical["D:|"].free[0], .logical["D:|"].total_size[0]' file.json

Output:

10178084864
21471686656
  • This works but in json data the drives can be from a to z. – user138530 Oct 13 '18 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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