# Filtering json from rest api call

**URL:** <https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714>\
**Category:** PowerShell Help\
**Created:** [August 18, 2015, 8:28am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714 "2015-08-18T08:28:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 18, 2015, 8:28am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/1 "2015-08-18T08:28:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vasken-h](https://avatars.discourse-cdn.com/v4/letter/v/cdc98d/32.png) [@vasken-h](https://forums.powershell.org/u/vasken-h)\
**Post date:** [August 18, 2015, 8:57am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/2 "2015-08-18T08:57:44Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [August 18, 2015, 9:19am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/3 "2015-08-18T09:19:48Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 18, 2015, 9:26am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/4 "2015-08-18T09:26:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 18, 2015, 9:28am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/5 "2015-08-18T09:28:10Z")

</div>

Cheers Bob,

I’ll give that a try too.

Best regards

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 18, 2015, 11:38am UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/6 "2015-08-18T11:38:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 18, 2015, 7:48pm UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/7 "2015-08-18T19:48:05Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/filtering-json-from-rest-api-call/4714/8 "2024-05-16T20:44:55Z")

</div>


