Store output diskspace df -h JSON
<p>I am attempting to gather basic disk space information from a server using a bash script, and store the output in JSON format. I am looking to record the available &amp; used disk space. </p>

<p>An example output of df -h:</p>

<pre><code>Filesystem                      Size  Used Avail Use% Mounted on
udev                            2.0G  4.0K  2.0G   1% /dev
tmpfs                           394M  288K  394M   1% /run
/dev/mapper/nodequery--vg-root   45G  1.4G   41G   4% /
none                            4.0K     0  4.0K   0% /sys/fs/cgroup
none                            5.0M     0  5.0M   0% /run/lock
none                            2.0G     0  2.0G   0% /run/shm
none                            100M     0  100M   0% /run/user
/dev/sda2                       237M   47M  178M  21% /boot
/dev/sda1                       511M  3.4M  508M   1% /boot/efi
</code></pre>

<p>As an example this is how I would like the final output to look. </p>

<pre><code>{
  "diskarray": [{
    "mount": "/dev/disk1",
    "spacetotal": "35GB",
    "spaceavail": "1GB"
  },
  {
    "mount": "/dev/disk2",
    "spacetotal": "35GB",
    "spaceavail": "4GB"
  }]
}
</code></pre>

<p>So far I've tried using awk:</p>

<pre><code>df -P -B 1 | grep '^/' | awk '{ print $1" "$2" "$3";" }'
</code></pre>

<p>with the following output:</p>

<pre><code>/dev/mapper/nodequery--vg-root 47710605312 1439592448;
/dev/sda2 247772160 48645120;
/dev/sda1 535805952 3538944;
</code></pre>

<p>But I'm not sure how I take that data and store it in the JSON format.</p>