CodexBloom - Programming Q&A Platform

how to retrieve specific property values from nested JSON in PowerShell 7.3 using ConvertFrom-Json

👀 Views: 2 💬 Answers: 1 📅 Created: 2025-05-31
powershell json api PowerShell

I'm updating my dependencies and I'm confused about I'm trying to implement Quick question that's been bugging me - I'm trying to parse a JSON response from an API call in PowerShell 7.3 and extract specific property values from a nested object... The JSON structure looks like this: ```json { "data": { "users": [ { "id": 1, "name": "John Doe", "contact": { "email": "john@example.com", "phone": "123-456-7890" } }, { "id": 2, "name": "Jane Smith", "contact": { "email": "jane@example.com", "phone": "098-765-4321" } } ] } } ``` I am using the following PowerShell code to extract the email addresses: ```powershell $response = Get-Content -Raw -Path 'path-to-your-json-file.json' | ConvertFrom-Json $emails = $response.data.users | ForEach-Object { $_.contact.email } $emails ``` However, when I run this, I receive an behavior message stating: `want to bind argument to parameter 'Path' because it is null.` I checked the file path and it seems correct. I also tried replacing `Get-Content` with `Invoke-RestMethod` to call the API directly, but I ended up with a similar scenario where the `$response` variable was empty. I’ve verified that the JSON is correctly formatted and I can view it using a JSON viewer. I'm not sure why the properties aren't being accessed correctly or why I'm working with this behavior. Any insights on how to properly retrieve these nested properties or troubleshoot this scenario would be greatly appreciated. My development environment is Windows. Is this even possible? Any ideas what could be causing this? I'm working on a microservice that needs to handle this. Could this be a known issue? Has anyone else encountered this?