hashtable with integers with convertto-json

Welcome Forums General PowerShell Q&A hashtable with integers with convertto-json

This topic contains 1 reply, has 2 voices, and was last updated by

9 months, 3 weeks ago.

  • Author
  • #101885

    Points: 39
    Rank: Member

    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 = ''
    $json_load = [ordered]@{
                    "portId" = "CL1-A"
                    "hostGroupName" = "Test_restapi_hostgroup2"
                    "hostModeOptions"= '[54,63]'
                    "hostMode"= "VMWARE_EX"
    $json_payload = $json_load|ConvertTo-Json

    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.

  • #101890

    Points: 1,154
    Helping Hand
    Rank: Community Hero

    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...

                "portId" = "CL1-A"
                "hostGroupName" = "Test_restapi_hostgroup2"
                "hostModeOptions"= 54,63
                "hostMode"= "VMWARE_EX"
              }  | ConvertTo-Json
    # Results
        "portId":  "CL1-A",
        "hostGroupName":  "Test_restapi_hostgroup2",
        "hostModeOptions":  [
        "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.

The topic ‘hashtable with integers with convertto-json’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort