Author Posts

June 6, 2018 at 3:50 am

I am trying to create a hashtable that i can then convert to a json body variable. When i do that it is wrapping all the values in quotes, but the integer value needs to look like [34,56] and not like "[34,56]"

Here is the syntax per the json examples:

{
"portId": "CL1-A",
"hostGroupName": "My_REST_API_HOST",
"hostModeOptions": [12,33],
"hostMode": "AIX"
}

Here is what i am using for my powershell code

#region Create HostGroup
$uri_hostgroup_Create = 'http://10.65.77.147:23450/ConfigurationManager/v1/objects/storages/800000056031/host-groups'
$json_load = [ordered]@{
                "portId" = "CL1-A"
                "hostGroupName" = "Test_restapi_hostgroup2"
                "hostModeOptions"= '[54,63]'
                "hostMode"= "VMWARE_EX"
              }  
$json_payload = $json_load|ConvertTo-Json
$json_payload

Here is the outcome:

{
"portId": "CL1-A",
"hostGroupName": "Test_restapi_hostgroup2",
"hostModeOptions": "[54,63]",
"hostMode": "VMWARE_EX"
}

Any thoughts on how i can get the values without the quotes. I assume its happening becouse it thinks its a string, but i cant find away to show it as a int.

Thanks in advance.

June 6, 2018 at 6:13 am

Nope that is not what that cmdlet does. It coverts what you send it to proper JSON. It's doing what it is told.

You are getting the quoted thing, because it's quoted in your ordered hash table. Thus it's a string at the start.

You need to lose the brackets and the quotes in that hash table. The convert will put them back automatically. Yet, note the moment you do that, they are seen as integers (your stated goal), but due to that change, they become an array of integers...

Clear-Host
[ordered]@{
            "portId" = "CL1-A"
            "hostGroupName" = "Test_restapi_hostgroup2"
            "hostModeOptions"= 54,63
            "hostMode"= "VMWARE_EX"
          }  | ConvertTo-Json

# Results

{
    "portId":  "CL1-A",
    "hostGroupName":  "Test_restapi_hostgroup2",
    "hostModeOptions":  [
                            54,
                            63
                        ],
    "hostMode":  "VMWARE_EX"
}

… and you seem to want to keep them on the same line. So, now you have to join those numbers back together, comma separated of course, if you want that format for your chosen reasons.