Author Posts

August 18, 2015 at 1:58 pm

Trying to format and filter a json response from a storage api rest call.

I can do the api call fine using the following command:

$volume023 =Invoke-WebRequest -Method Get -Credential $cred -Uri $resource -Header @{ "X-Access-Key" = $apiKey }

getting the following response:

{
    "response":  {
                     "status":  0,
                     "volume":  {
                                    "name":  "volume-00000009",
                                    "display_name":  "volume023",
                                    "cg_display_name":  "volume023",
                                    "cg_name":  "cg-0000001e",
                                    "cg_user_created":  "NO",
                                    "pool_display_name":  "SMC01_Pool2",
                                    "pool_name":  "pool-00000002",
                                    "status":  "In-use",
                                    "virtual_capacity":  5000,
                                    "server_name":  "BRIPVWOW001",
                                    "data_type":  "File-System",
                                    "thin":  "NO",
                                    "encryption":  "NO",
                                    "total_capacity":  4999.0234375,
                                    "available_capacity":  1792.84472656,
                                    "allocated_capacity":  3209.3359375,
                                    "data_copies_capacity":  491.987060547,
                                    "access_type":  "SMB",
                                    "read_only":  "NO",
                                    "nfs_export_path":  "10.200.150.90:/export/volume023",
                                    "smb_export_path":  "\\\\10.200.150.90\\volume023",
                                    "mount_sync":  "YES",
                                    "atime_update":  "NO",
                                    "ext_metering":  "NO",
                                    "smb_only":  "NO",
                                    "smb_guest":  "NO",
                                    "smb_windows_acl":  "YES",
                                    "smb_map_archive":  "YES",
                                    "smb_file_create_mask":  "0744",
                                    "smb_dir_create_mask":  "0755",
                                    "smb_aio_size":  1,
                                    "capacity_history":  60,
                                    "alert_mode":  360,
                                    "target":  null,
                                    "lun":  null,
                                    "cache":  null,
                                    "tenant_id":  12,
                                    "read_iops_limit":  "0",
                                    "read_mbps_limit":  "0",
                                    "write_iops_limit":  "0",
                                    "write_mbps_limit":  "0",
                                    "thresholds":  null,
                                    "created_at":  "2012-06-12T08:57:16+00:00",
                                    "modified_at":  "2015-07-20T19:43:59+00:00"
                                }
                 }
}

I'd like to filter this to just:

"total_capacity": 4999.0234375
"available_capacity": 1792.84472656,

and pipe it as a variable so it can be embedded in an automated email

Any help here would be greatly appreciated

August 18, 2015 at 2:27 pm

Let's assume you take that JSON and store it as a string (I'd recommend a HereString) called $json

You can then invoke ConvertFrom-Json and access individual values as if they were properties.

$x = $json | ConvertFrom-Json
Write-Host "Total Capacity is:" $x.response.volume.total_capacity
Write-Host "Available Capacity is:" $x.response.volume.available_capacity

August 18, 2015 at 2:49 pm

You just beat me to it. Here was my approach in setting up for an email:

$data = Get-Content -Path .\data.json | ConvertFrom-Json 
$msg = @"
Available Capacity = $("{0:N1}" -f $data.response.volume.available_capacity)
Total Capacity     = $("{0:N1}" -f $data.response.volume.total_capacity)
"@
$msg

August 18, 2015 at 2:56 pm

Hi Vasken,

Thanks for this, works a treat.

I'm using write-output instead of write-host
$total =Write-output "Total Capacity is:" $x.response.volume.total_capacity
$available =Write-output "Available Capacity is:" $x.response.volume.available_capacity

Many thanks

Kevin

August 18, 2015 at 2:58 pm

Cheers Bob,

I'll give that a try too.

Best regards

August 18, 2015 at 5:08 pm

If you use Invoke-RestMethod instead of Invoke-WebRequest, you'll just get back an object. (Same result as if you'd used ConvertFrom-Json).

August 19, 2015 at 1:18 am

Thanks Dave,

I wasn't sure what the difference between Invoke-WebRequest and Invoke-RestMethod were. I've changed to Invoke-RestMethod and have removed | ConvertFrom-Json

$x = $json
$total =Write-output "Total Capacity is:" $x.response.volume.total_capacity
$available =Write-output "Available Capacity is:" $x.response.volume.available_capacity