skip to Main Content

I have the using the REST API to get the result, then I get the Json file, ApiConnectionjson:

$token = $(Token)
          
$header = @{authorization = "Bearer $token"}

$url = "https://management.azure.com/xxxx?api-version=2018-07-01-preview"
          
$result = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header 
         
$ApiConnectionjson = $result | ConvertTo-Json -depth 100

Write-Host "$ApiConnectionjson"

Now, I get the Json file via above powershell. No need to pay attention to the above code, it is not the key point.

The ApiConnectionjson like:

{
    "kind":  "V2",
    "properties":  {
                       "displayName":  "xxxx",
                       "authenticatedUser":  {
                                                 "name":  "xxx"
                                             },
                       "overallStatus":  "Connected",
                       "statuses":  [
                                        {
                                            "status":  "Connected"
                                        }
                                    ],
                       "connectionState":  "Enabled",
   ...
   ...
}

Now I want add key value "apiVersion": "2016-06-01", in the first line to the json, like:

{
    "apiVersion": "2016-06-01",
    "kind":  "V2",
    "properties":  {
                       "displayName":  "xxxx",
                       "authenticatedUser":  {
                                                 "name":  "xxx"
                                             },
                       "overallStatus":  "Connected",
                       "statuses":  [
                                        {
                                            "status":  "Connected"
                                        }
                                    ],
                       "connectionState":  "Enabled",
   ...
   ...
}

I have tried the method in this thread: but my powershell does not support the method pscustomobject.

Edit:
My test code:

$ApiConnectionjson = $ApiConnectionjson | ConvertFrom-Json

$ApiConnectionjson += [pscustomobject] @{ apiVersion = '2016-06-01' }

$ApiConnectionjson  | ConvertTo-Json -Depth 10

Error message:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’.
At D:a_tempazureclitaskscript1679387440203_inlinescript.ps1:17 char:1

3

Answers


  1. Please see the below code, this applies the new property using the add-member cmdlet prior to converting it to JSON as the object itself is easier to manipulate, then it uses the select cmdlet to force the position of the property to be the 1st value.

    $versionNo = "x.x.x.x"
    
    $token = $(Token)
          
    $header = @{authorization = "Bearer $token"}
    
    $url = "https://management.azure.com/xxxx?api-version=2018-07-01-preview"
          
    $result = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header 
         
    $ApiConnectionjson = $result | Add-Member -NotePropertyName "apiVersion" -NotePropertyValue $versionNo | Select apiVersion, * | ConvertTo-Json -depth 100
    
    Write-Host "$ApiConnectionjson"
    
    Login or Signup to reply.
  2. Unfortunately, Invoke-RestMethod doesn’t have a -AsHashTable parameter which would make things easier (see: #19378 Invoke-RestMethod should have an -AsHashTable switch). In other words, you will need to use the Add-Member cmdlet for this:

    $Json = @'
    {
        "kind":  "V2",
        "properties":  {
        "displayName":  "xxxx",
        "authenticatedUser": {
            "name": "xxx"
        },
        "overallStatus":  "Connected",
        "statuses": [
            {
                "status": "Connected"
            }
        ],
        "connectionState": "Enabled"
        }
    }
    '@
    
    $Json |ConvertFrom-Json |
      Add-Member -MemberType NoteProperty -Name "apiVersion" -Value "2016-06-01" -PassThru |
      ConvertTo-Json -Depth 9
    
    {
      "kind": "V2",
      "properties": {
        "displayName": "xxxx",
        "authenticatedUser": {
          "name": "xxx"
        },
        "overallStatus": "Connected",
        "statuses": [
          {
            "status": "Connected"
          }
        ],
        "connectionState": "Enabled"
      },
      "apiVersion": "2016-06-01"
    }
    

    In case you want "apiVersion": "2016-06-01" to be added in the first line, I would replace the concerned (top) object layer:

    $Data = $Json |ConvertFrom-Json
    $Data = [PSCustomObject]@{
        apiVersion = '2016-06-01'
        kind       = $Data.kind
        properties = $Data.properties
    }
    $Data |ConvertTo-Json -Depth 9
    

    Or use the Select-Object with a calculated property:

    $Json |ConvertFrom-Json |
        Select-Object @{ n='apiVersion'; e={ '2016-06-01' } }, kind, properties |
        ConvertTo-Json -Depth 9
    
    Login or Signup to reply.
  3. I try to explain the error as other answers are already given:

    $jsonStr = @'
    [{
        "kind":  "V2",
        "properties": {
            "displayName":  "xxxx",
            "authenticatedUser": {
                "name":  "xxx"
            },
            "overallStatus":  "Connected",
            "statuses":  [
            {
                "status":  "Connected"
            }
            ],
            "connectionState":  "Enabled"
            }
        }
    ]
    '@
    
    $json = $jsonStr | ConvertFrom-Json
    
    # this now works because the insertion point at the root is now an array:
    $json += [PSCustomObject] @{ apiVersion = 'V1.2.3' }
    

    as long as it’s an collection the += is unknown.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search