0

I have the following Function that creates a JSON:

Function New-JSONTest {
    Param (
        [Parameter( ParameterSetName = "NewConfig" )]
        [switch]$NewConfig
    )

    $PSCustomObject = switch ($PSCmdLet.ParameterSetName) {
        NewConfig { 
            [PSCustomObject]@{
                Menus = @(
                    [PSCustomObject]@{
                        Name = "Produktauswahl"
                        IsEnabledByDefault = $true
                        IsVisibleByDefault = $true
                        IsEnabledIf = ""
                        IsVisibleIf = ""
                        OnClickAction = [PSCustomObject]@{
                            ActionCode = 3
                            Parameter = @(
                                [PSCustomObject]@{
                                    ParameterName = "TestInput.Menü"
                                    ParameterType = "string"
                                    ParameterValue = "Menus.Name"
                                }
                            )
                        }
                    },
                    [PSCustomObject]@{
                        Name = "Grundkonfiguration"
                        IsEnabledByDefault = $false
                        IsVisibleByDefault = $true
                        IsEnabledIf = ""
                        IsVisibleIf = ""
                        OnClickAction = [PSCustomObject]@{
                            ActionCode = 3
                            Parameter = @(
                                [PSCustomObject]@{
                                    ParameterName = "TestInput.Menü"
                                    ParameterType = "string"
                                    ParameterValue = "Menus.Name"
                                }
                            )
                        }
                    },
                    [PSCustomObject]@{
                        Name = "Zubehörkonfiguration"
                        IsEnabledByDefault = $false
                        IsVisibleByDefault = $true
                        IsEnabledIf = ""
                        IsVisibleIf = ""
                        OnClickAction = [PSCustomObject]@{
                            ActionCode = 3
                            Parameter = @(
                                [PSCustomObject]@{
                                    ParameterName = "TestInput.Menü"
                                    ParameterType = "string"
                                    ParameterValue = "Menus.Name"
                                }
                            )
                        }
                    },
                    [PSCustomObject]@{
                        Name = "Konfiguration abschliessen"
                        IsEnabledByDefault = $false
                        IsVisibleByDefault = $true
                        IsEnabledIf = ""
                        IsVisibleIf = ""
                        OnClickAction = [PSCustomObject]@{
                            ActionCode = 3
                            Parameter = @(
                                [PSCustomObject]@{
                                    ParameterName = "TestInput.Menü"
                                    ParameterType = "string"
                                    ParameterValue = "Menus.Name"
                                }
                            )
                        }
                    }
                )
            }
        }
    }

    # Return as JSON
    $PSCustomObject | ConvertTo-Json
}

As you can see, in each Menus Object, I have a OnClickAction Object with another nested PSCustomObject in an array.

When I run the code, each Menu I get returned looks like this:

{
  "Name": "Produktauswahl",
  "IsEnabledByDefault": true,
  "IsVisibleByDefault": true,
  "IsEnabledIf": "",
  "IsVisibleIf": "",
  "OnClickAction": "@{ActionCode=3; Parameter=System.Object[]}"
}

Can I somehow force it to Display my OnClickAction also in an expanded JSON, instead of an array that is not readable? Basically what I need in the End is this:

{
    "Menus": [
        {
            "Name": "Produktauswahl",
            "IsEnabledByDefault": true,
            "IsVisibleByDefault": true,
            "IsEnabledIf": "",
            "IsVisibleIf": "",
            "OnClickAction": {
                "ActionCode": "3",
                "Parameter": [
                    "ParameterName": "TestInput.Menü"
                    "ParameterType": "string"
                    "ParameterValue": "Menus.Name"  
                ]
            }
        }
    ]
}

Is this possible or am I misunderstanding something?

SimonS
  • 1,891
  • 1
  • 29
  • 53

1 Answers1

0

The default depth of ConvertTo-Json is limited to 2 in the default, but you can set the depth with -Depth. A depth of 5 should be ok for your Json:

$PSCustomObject | ConvertTo-Json -Depth 5
T-Me
  • 1,814
  • 1
  • 9
  • 22