Nitin Jain
Nitin Jain

Reputation: 188

Azure pipelines -Add new element in json array VSTS pipelines (Appsettings.json)

Is this possible to add a new element in an array of appsetting.json in Azure Release Pipeline?

In appsetting.json I have array variable which I need to fill with another element during deployment through Azure Pipeline.

  "Array": [
                {
                    "Name": "AD1",
                    "IsDefault": "true",
                    "IdPEntityId": "URL1",
                    "Metadata": "XMLpath1"
                },
                {
                    "Name": "AD2",
                    "IsDefault": "false",
                    "IdPEntityId": "URL2",
                    "Metadata": "XMLPath2"
                }
]

Here in the above JSON array I need to add another one elemental last position (array-Index:2).

Upvotes: 2

Views: 5965

Answers (2)

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51153

You could use JSON variable substitution. This feature substitutes values in the JSON configuration files. It overrides the values in the specified JSON configuration files (for example, appsettings.json) with the values matching names of release pipeline and stage variables.

When in "Deploy Azure App Service" release task you should see a "File Transforms and Variable Substitution" section. In here you will supply the path to the json file you want to swap variable values.

[![enter image description here][1]][1]

Then you just need to define the required substitution values in release pipeline or stage variables. From here you can add the json property you want to modify as a variable.

[![enter image description here][2]][2]

Finally after the transformation, the JSON will contain new. Azure DevOps will then swap out these values for you when deploying.

More details you could refer our official tutorial here: [File transforms and variable substitution reference][3]


Update:

It only works to adjust existing entries in the appsettings.json files, it doesn't seem to be able to add any new one. You could also take a look at the JSON variable substitution notes

Variable substitution is applied for only the JSON keys predefined in the object hierarchy. It does not create new keys.

As a workaround, you could choose to use the File Creator extension:https://marketplace.visualstudio.com/items?itemName=eliostruyf.build-task to push the whole new appsettings.json file in the pipeline.

Update2

OP finally moved with PS script written by him to add new elements in Arrays of Appsettings.json

Upvotes: -1

Nitin Jain
Nitin Jain

Reputation: 188

[CmdletBinding()]

param(
    [string] $AdName,
    [bool]   $AdIsDefault,
    [string] $AdIdPEntityId, 
    [string] $AdMetadata,
    [string] $AppSettingFilePath  
)
clear-Host

Write-Host 'Updating appsettings.json...' -ForegroundColor Yellow

function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
  $indent = 0;
  ($json -Split '\n' |
    % {
      if ($_ -match '[\}\]]') {
        # This line contains  ] or }, decrement the indentation level
        $indent--
      }
      $line = (' ' * $indent * 2) + $_.TrimStart().Replace(':  ', ': ')
      if ($_ -match '[\{\[]') {
        # This line contains [ or {, increment the indentation level
        $indent++
      }
      $line
  }) -Join "`n"
}

$JsonDataAdd=@"
{
                    "Name":"$AdName",
                    "IsDefault": "$AdIsDefault",
                    "IdPEntityId":"$AdIdPEntityId",
                    "Metadata": "$AdMetadata"
}
"@
Write-Host ' Active directory details :' -ForegroundColor Yellow

Write-Host `n  $JsonDataAdd -ForegroundColor Green

$jsonData = Get-Content "$AppSettingFilePath" | Out-String | ConvertFrom-Json -ErrorAction Stop


$jsonData.IdentitySettings.ExternalProviders.Saml2Providers += (ConvertFrom-Json $JsonDataAdd)


$jsonData | ConvertTo-Json -Depth 10 |  Format-Json | Set-Content "$AppSettingFilePath" -Encoding UTF8

Write-Host 'Successfully Updated -appSettings.json  !' -ForegroundColor Yellow

Upvotes: 2

Related Questions