pyem
pyem

Reputation: 13

Building a nested dictionary/hash tables in powershell displays "System.Collections.Hashtable"

I'm trying to create a body for a webrequest which is in the form of a nested dictionary.

$body +=@{}
$body["tables"] = @()
$body["tables"] += @{}
$body["tables"][0]["id"] += @{}
$body["tables"][0]["id"]["columnId"] = "1"
$body["tables"][0]["id"]["fieldType"] = "1"
$body["tables"][0]["textFilter"] = @{"value" = "123"}
$body2Json = ConvertTo-Json $body

When I try to print this, I get the following:

{
    "tables":  [
                   {
                       "id":  "System.Collections.Hashtable",
                       "textFilter":  "System.Collections.Hashtable"
                   }
               ]
}

Not sure what am I doing wrong here, still new to powershell

Upvotes: 1

Views: 1199

Answers (1)

FoxDeploy
FoxDeploy

Reputation: 13547

You created a pretty complex, multi-node PowerShell object, but the ConvertTo-Json cmdlet only converts the first two levels of depth before it stops.

Fortunately, You can control this behavior with the -Depth parameter like so:

ConvertTo-Json $body -Depth 5
{
    "tables": [{
        "id": {
            "columnId": "1",
            "fieldType": "1"
        },
        "textFilter": {
            "value": "123"
        }
    }]
}

Upvotes: 1

Related Questions