Search code examples
powershellpscustomobjectobject-graph

Powershell Selecting NoteProperty Type Objects From Object


I am working with deeply nested JSON and, after convertfrom-json, need to be able to traverse through various parts of the object which the convertfrom-json cmdlet generates.

I have no way of knowing in advance what property names may or may not be inside the object, as far as I can tell, there are hundreds of different possible properties. Fortunately the one thing I am seeing that helps is that each of the properties I care about is of type "NoteProperty".

Here is an example:

TypeName: System.Management.Automation.PSCustomObject

Name               MemberType   Definition
----               ----------   ----------
Equals             Method       bool Equals(System.Object obj)
GetHashCode        Method       int GetHashCode()
GetType            Method       type GetType()
ToString           Method       string ToString()
definition         NoteProperty System.Management.Automation.PSCustomObject definition=@{$schema=https://schema.management.azure.com/providers/Microsof... 
integrationAccount NoteProperty System.Management.Automation.PSCustomObject integrationAccount=@{id=[parameters('integrationAccounts_xxx_integration... 
parameters         NoteProperty System.Management.Automation.PSCustomObject parameters=@{$connections=}
state              NoteProperty string state=Enabled

So I thought it would be simple to create a function which would select only the objects, for the level currently being processed, which are of 'MemberType' 'NoteProperty'.

I have tried piping the object to:

where-object { $_.MemberType -eq "NoteProperty" }

Nope.

I have tried select-object in various forms too but can't seem to select just what I need. I found an old article from the Scripting guys about using Labels and Expressions - but that seems like overkill, no? Can someone point me to simple way to select just the NoteProperty items?

Thanks!


Solution

  • You could use the hidden .psobject.properties to iterate over the members.

    $json = @'
    {
      "users": [
        {
          "userId": 1,
          "firstName": "Krish",
          "lastName": "Lee",
          "phoneNumber": "123456",
          "emailAddress": "[email protected]"
        },
        {
          "userId": 2,
          "firstName": "racks",
          "lastName": "jacson",
          "phoneNumber": "123456",
          "emailAddress": "[email protected]"
        }
      ]
    }
    '@ | ConvertFrom-Json
    
    $json | foreach {
        $_.psobject.properties | foreach {
            Write-Host Property Name: $_.name
            Write-Host Values: $_.value
        }
    } 
    

    You can keep going as needed.

    $json | foreach {
        $_.psobject.properties | foreach {
            $_.value | foreach {
                $_.psobject.properties | foreach {
                    write-host Property name: $_.name
                    write-host Property value: $_.value
                }
            }
        }
    }
    
    Property name: userId
    Property value: 1
    Property name: firstName
    Property value: Krish
    Property name: lastName
    Property value: Lee
    Property name: phoneNumber
    Property value: 123456
    Property name: emailAddress
    Property value: [email protected]
    Property name: userId
    Property value: 2
    Property name: firstName
    Property value: racks
    Property name: lastName
    Property value: jacson
    Property name: phoneNumber
    Property value: 123456
    Property name: emailAddress
    Property value: [email protected]