Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell – How do I iterate a PSCustomObject nested object?

I feel like this is something simple and I’m just not getting it, and I’m not sure if my explanation is great.
I have this below JSON file, and I want to get "each App" (App1, App2, App3) under the "New" object

In this script line below I’m essentially trying to replace "TestApp2" with some variable. I guess I’m trying to get TestApp2 as an object without knowing the name.
And I realize that the foreach loop doesn’t do anything right now

Write-Host $object.Value.TestApp2.reply_urls

JSON:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "New": {
    "App1": {
      "reply_urls": [
        "https://testapp1url1"
      ]
    },
    "App2": {
      "reply_urls": [
        "https://testapp2url1",
        "https://testapp2url2"
      ]
    },
    "App3": {
      "reply_urls": [
        "https://testapp3url1",
        "https://testapp3url2",
        "https://testapp3url3"
      ]
    }
  },
  "Remove": {
      "object_id": [
        ""
      ]
  }
}

Script:

$inputFile = Get-Content -Path $inputFilePath -Raw | ConvertFrom-Json
foreach ($object in $inputFile.PsObject.Properties)
{
    switch ($object.Name)
    {
        New
        {
            foreach ($app in $object.Value)
            {
               Write-Host $object.Value.TestApp2.reply_urls
               # essentially want to replace this line with something like
               # Write-Host $app.reply_urls
            }
        }

        Remove
        {
        }
    }
}

Output:

https://testapp2url1 https://testapp2url2

>Solution :

You can access the object’s PSObject.Properties to get the property Names and property Values, which you can use to iterate over.

For example:

foreach($obj in $json.New.PSObject.Properties) {
    $out = [ordered]@{ App = $obj.Name }
    foreach($url in $obj.Value.PSObject.Properties) {
        $out[$url.Name] = $url.Value
    }
    [pscustomobject] $out
}

Produces the following output:

App  reply_urls
---  ----------
App1 {https://testapp1url1}
App2 {https://testapp2url1, https://testapp2url2}
App3 {https://testapp3url1, https://testapp3url2, https://testapp3url3}

If you just want to output the URL you can skip the construction of the PSCustomObject:

foreach($obj in $json.New.PSObject.Properties) {
    foreach($url in $obj.Value.PSObject.Properties) {
        $url.Value
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading