Calling a class an array in PowerShell

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 7 months, 3 weeks ago.

  • Author
    Posts
  • #54892
    Profile photo of Krishna Chaitanya
    Krishna Chaitanya
    Participant

    I am creating a class to build a Json object I would like to call another class as an array how can I do that

    Class A
    {
    [string] $aObject
    [B] $aObject // this didn't worked for me
    }

    Class B
    {
    [string] $bObject
    }

    Class c
    {
    [string] $cObject
    }

    I need class B and C as arrays in class A

    I am trying to loop this recursively either by Json or by creating an object

    {
    "cluster": [
    {
    "id": "cluster1.1",
    "color": "blue",
    "segment": [
    {
    "id": "segment1.1",
    "color": "green"
    }
    ]
    },
    {
    "id": "cluster1.2",
    "color": [
    "blue",
    "red"
    ],
    "segment": [
    {
    "id": "segment1.2",
    "color": "Yellow"
    }
    ]
    },
    {
    "id": "cluster1.3",
    "color": "Orange",
    "segment": [
    {
    "id": "cluster1.3",
    "color": "black"
    },
    {
    "id": "cluster1.4",
    "color": "Green"
    },
    {
    "id": "cluster1.5",
    "color": "red"
    }
    ]
    },
    {
    "id": "cluster1.4",
    "color": [
    "blue",
    "red"
    ],
    "segment": [
    {
    "id": "cluster1.4",
    "color": "red"
    },
    {
    "id": "cluster1.5",
    "color": "blue"
    },
    {
    "id": "cluster1.6",
    "color": "Yellow"
    }
    ]
    }
    ]
    }

  • #54923
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    You don't need to build this. ConvertFrom-Json already exists.

    Example:

    $data = @'
    {
    "cluster": [
    {
    "id": "cluster1.1",
    "color": "blue",
    "segment": [
    {
    "id": "segment1.1",
    "color": "green"
    }
    ]
    },
    {
    "id": "cluster1.2",
    "color": [
    "blue",
    "red"
    ],
    "segment": [
    {
    "id": "segment1.2",
    "color": "Yellow"
    }
    ]
    },
    {
    "id": "cluster1.3",
    "color": "Orange",
    "segment": [
    {
    "id": "cluster1.3",
    "color": "black"
    },
    {
    "id": "cluster1.4",
    "color": "Green"
    },
    {
    "id": "cluster1.5",
    "color": "red"
    }
    ]
    },
    {
    "id": "cluster1.4",
    "color": [
    "blue",
    "red"
    ],
    "segment": [
    {
    "id": "cluster1.4",
    "color": "red"
    },
    {
    "id": "cluster1.5",
    "color": "blue"
    },
    {
    "id": "cluster1.6",
    "color": "Yellow"
    }
    ]
    }
    ]
    }
    '@
    
    $json = ConvertFrom-Json $data
    $json.cluster

    Results:

    id         color       segment                                                                                    
    --         -----       -------                                                                                    
    cluster1.1 blue        {@{id=segment1.1; color=green}}                                                            
    cluster1.2 {blue, red} {@{id=segment1.2; color=Yellow}}                                                           
    cluster1.3 Orange      {@{id=cluster1.3; color=black}, @{id=cluster1.4; color=Green}, @{id=cluster1.5; color=red}}
    cluster1.4 {blue, red} {@{id=cluster1.4; color=red}, @{id=cluster1.5; color=blue}, @{id=cluster1.6; color=Yellow}}

You must be logged in to reply to this topic.