Tuesday, April 23, 2024
HomePowershellPowerShell - Changing superior object to flat object

PowerShell – Changing superior object to flat object


For those who don’t love utilizing modules, you should use code immediately.

Operate ConvertTo-FlatObject {
    <#
    .SYNOPSIS
    Flattends a nested object right into a single stage object.

    .DESCRIPTION
    Flattends a nested object right into a single stage object.

    .PARAMETER Objects
    The item (or objects) to be flatten.

    .PARAMETER Separator
    The separator used between the recursive property names

    .PARAMETER Base
    The primary index identify of an embedded array:
    - 1, arrays will likely be 1 based mostly: <Dad or mum>.1, <Dad or mum>.2, <Dad or mum>.3, …
    - 0, arrays will likely be 0 based mostly: <Dad or mum>.0, <Dad or mum>.1, <Dad or mum>.2, …
    - "", the primary merchandise in an array will likely be unnamed and than adopted with 1: <Dad or mum>, <Dad or mum>.1, <Dad or mum>.2, …

    .PARAMETER Depth
    The maximal depth of flattening a recursive property. Any damaging worth will end in a limiteless depth and will trigger a infinitive loop.

    .PARAMETER Uncut
    The maximal depth of flattening a recursive property. Any damaging worth will end in a limiteless depth and will trigger a infinitive loop.

    .EXAMPLE
    $Object3 = [PSCustomObject] @{
        "Identify"    = "Przemyslaw Klys"
        "Age"     = "30"
        "Deal with" = @{
            "Avenue"  = "Kwiatowa"
            "Metropolis"    = "Warszawa"

            "Nation" = [ordered] @{
                "Identify" = "Poland"
            }
            Record      = @(
                [PSCustomObject] @{
                    "Identify" = "Adam Klys"
                    "Age"  = "32"
                }
                [PSCustomObject] @{
                    "Identify" = "Justyna Klys"
                    "Age"  = "33"
                }
                [PSCustomObject] @{
                    "Identify" = "Justyna Klys"
                    "Age"  = 30
                }
                [PSCustomObject] @{
                    "Identify" = "Justyna Klys"
                    "Age"  = $null
                }
            )
        }
        ListTest  = @(
            [PSCustomObject] @{
                "Identify" = "Sława Klys"
                "Age"  = "33"
            }
        )
    }

    $Object3 | ConvertTo-FlatObject

    .NOTES
    Primarily based on https://powersnippets.com/convertto-flatobject/
    #>
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeLine)][Object[]]$Objects,
        [String]$Separator = ".",
        [ValidateSet("", 0, 1)]$Base = 1,
        [int]$Depth = 5,
        [Parameter(DontShow)][String[]]$Path,
        [Parameter(DontShow)][System.Collections.IDictionary] $OutputObject
    )
    Start {
        $InputObjects = [System.Collections.Generic.List[Object]]::new()
    }
    Course of {
        foreach ($O in $Objects) {
            $InputObjects.Add($O)
        }
    }
    Finish {
        If ($PSBoundParameters.ContainsKey("OutputObject")) {
            $Object = $InputObjects[0]
            $Iterate = [ordered] @{}
            if ($null -eq $Object) {
                #Write-Verbose -Message "ConvertTo-FlatObject - Object is null"
            } elseif ($Object.GetType().Identify -in 'String', 'DateTime', 'TimeSpan', 'Model', 'Enum') {
                $Object = $Object.ToString()
            } elseif ($Depth) {
                $Depth--
                If ($Object -is [System.Collections.IDictionary]) {
                    $Iterate = $Object
                } elseif ($Object -is [Array] -or $Object -is [System.Collections.IEnumerable]) {
                    $i = $Base
                    foreach ($Merchandise in $Object.GetEnumerator()) {
                        $Iterate["$i"] = $Merchandise
                        $i += 1
                    }
                } else {
                    foreach ($Prop in $Object.PSObject.Properties) {
                        if ($Prop.IsGettable) {
                            $Iterate["$($Prop.Name)"] = $Object.$($Prop.Identify)
                        }
                    }
                }
            }
            If ($Iterate.Keys.Rely) {
                foreach ($Key in $Iterate.Keys) {
                    ConvertTo-FlatObject -Objects @(, $Iterate["$Key"]) -Separator $Separator -Base $Base -Depth $Depth -Path ($Path + $Key) -OutputObject $OutputObject
                }
            } else {
                $Property = $Path -Be part of $Separator
                $OutputObject[$Property] = $Object
            }
        } elseif ($InputObjects.Rely -gt 0) {
            foreach ($ItemObject in $InputObjects) {
                $OutputObject = [ordered]@{}
                ConvertTo-FlatObject -Objects @(, $ItemObject) -Separator $Separator -Base $Base -Depth $Depth -Path $Path -OutputObject $OutputObject
                [PSCustomObject] $OutputObject
            }
        }
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments