Search code examples
powershellnested-object

How to access nested object's absolute value in powershell


Hi I have stored some data in nested object format..Something like this

$group={
    "share":  {
                  "users":  [
                              {"name" : james
                                "id" :27
                              },
                              {"name" : jeni
                                "id" :18
                              },
                            ]
              }
}

when I try to retrieve the value of share or users it returns null

The output for pinging $group is

{
    "share":  {
                  "users":  [
                                "System.Collections.Hashtable",
                                "System.Collections.Hashtable"
                            ]
              }
}

How do I retrieve the value of users


Solution

  • Is this what you are after?

    $group = @'
    {
        "share":{
            "users":[
                {
                    "name":"james",
                     "id": "27"
                },
                {
                    "name":"jeni",
                    "id":"18"
                }
            ]
        }
    }
    '@
    
    $group | 
    ConvertFrom-Json
    
    <#
    share                   
    -----                   
    @{users=System.Object[]}
    #>
    
    
    $group | 
    ConvertFrom-Json | 
    Select -ExpandProperty Share
    
    <#
    users                                      
    -----                                      
    {@{name=james; id=27}, @{name=jeni; id=18}}
    #>
    
    
    $group | 
    ConvertFrom-Json | 
    Select -ExpandProperty Share | 
    Select -ExpandProperty Users
    
    <#
    name  id
    ----  --
    james 27
    jeni  18
    #>
    
    
    $group | 
    ConvertFrom-Json | 
    Select -ExpandProperty Share | 
    Select -ExpandProperty Users | 
    Select Name
    
    <#
    name 
    ---- 
    james
    jeni
    #>
    
    
    $group | 
    ConvertFrom-Json | 
    Select -ExpandProperty Share | 
    Select -ExpandProperty Users | 
    Select id
    
    <#
    id
    --
    27
    18
    #>
    
    
    ($group | 
    ConvertFrom-Json | 
    Select -ExpandProperty Share | 
    Select -ExpandProperty Users)[0]
    
    <#
    name  id
    ----  --
    james 27
    #>
    

    *As per my comment *

    # Dot reference the object
    
    $group = @'
    {
        "share":{
            "users":[
                {
                    "name":"james",
                     "id": "27"
                },
                {
                    "name":"jeni",
                    "id":"18"
                }
            ]
        }
    }
    '@
    
    
    (($group | ConvertFrom-Json)).share
    
    <#
    users                                      
    -----                                      
    {@{name=james; id=27}, @{name=jeni; id=18}}
    #>
    
    
    
    (($group | ConvertFrom-Json)).share.Users
    
    <#
    name  id
    ----  --
    james 27
    jeni  18
    #>
    
    
    
    (($group | ConvertFrom-Json)).share.Users.Name
    <#
    james
    jeni
    #>
    
    (($group | ConvertFrom-Json)).share.Users.id
    <#
    27
    18
    #>
    
    (($group | ConvertFrom-Json)).share.Users[0]
    
    <#
    name  id
    ----  --
    james 27
    #>
    
    
    
    (($group | ConvertFrom-Json)).share.Users | Select-Object -First 1
    
    <#
    name  id
    ----  --
    james 27
    #>