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.0total_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" } } } 

    3 Answers 3

    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 
    3
    • 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.jsonerror: 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 errorCommentedOct 13, 2018 at 17:48
    • I am using jq 1.5. What does jq --version say?
      – meuh
      CommentedOct 13, 2018 at 18:01
    • jq -Vjq-1.4-1-e73951f for this it doesn't works jq -Vjq-1.5-1-a5b5cbe for this version it worksCommentedOct 15, 2018 at 3:13
    0

    Use this :

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

    Output:

    10178084864 21471686656 
    1
    • This works but in json data the drives can be from a to z.CommentedOct 13, 2018 at 17:52
    0

    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.

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.