Filtering json from rest api call

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Kevin Kevin 1 year, 2 months ago.

  • Author
  • #28730
    Profile photo of Kevin

    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":  "",
                                        "smb_export_path":  "\\\\\\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

  • #28731
    Profile photo of Vasken

    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
  • #28732
    Profile photo of Bob McCoy
    Bob McCoy

    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)
  • #28733
    Profile photo of Kevin

    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


  • #28734
    Profile photo of Kevin

    Cheers Bob,

    I'll give that a try too.

    Best regards

  • #28736
    Profile photo of Dave Wyatt
    Dave Wyatt

    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).

  • #28743
    Profile photo of Kevin

    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

You must be logged in to reply to this topic.