CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Retrieving JSON Data from a REST API with Nested Objects

๐Ÿ‘€ Views: 65 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-14
PowerShell REST JSON

I'm not sure how to approach Hey everyone, I'm running into an issue that's driving me crazy... I'm having trouble retrieving JSON data from a REST API using PowerShell 7.3, specifically when it comes to handling nested objects in the response. I can successfully make the API call using `Invoke-RestMethod`, but when I try to access nested properties, I encounter issues. For example, hereโ€™s the code I'm using to call the API and parse the response: ```powershell $uri = "https://api.example.com/data" $response = Invoke-RestMethod -Uri $uri -Method Get ``` The structure of the JSON response looks something like this: ```json { "outer": { "inner": { "value": "example" } } } ``` When I try to access the nested property like this: ```powershell $value = $response.outer.inner.value ``` I'm getting the behavior message: `want to access property "inner" on the NULL value`. I've confirmed that the API call is successful since I can see the complete response when I output `$response` directly. Iโ€™ve also verified that the property names match the JSON structure, but Iโ€™m still working with this scenario. Iโ€™ve tried using `ConvertFrom-Json` as well: ```powershell $jsonResponse = $response | ConvertFrom-Json $value = $jsonResponse.outer.inner.value ``` However, the same behavior continues. It seems that the nested structure is causing PowerShell to not recognize the properties correctly. I'm not sure if I need to handle this differently or if there's a specific way to access nested properties in PowerShell 7.3. Any insights or solutions would be greatly appreciated! My development environment is macOS. I'd really appreciate any guidance on this. Am I missing something obvious?