1

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

James Z
  • 12,104
  • 10
  • 27
  • 43
Nitin Jain
  • 89
  • 1
  • 12

2 Answers2

2
[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
Nitin Jain
  • 89
  • 1
  • 12
0

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

Nitin Jain
  • 89
  • 1
  • 12
PatrickLu-MSFT
  • 47,272
  • 4
  • 29
  • 59
  • **You can override elements within an array or add additional elements** Do you mean that I can insert new element in existing Array of .Json file ? In Azure Pipeline variables with Name : Array.1 Value: Json String But this configuration doesn't work cause of Array.1 (Index-1) is not exist in json file. so it doesn't change anything. Where am I missing ..? – Nitin Jain Jan 30 '20 at 14:39
  • 1
    @NitinJain Sorry for the misunderstanding. 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](https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution?view=azure-devops#json-variable-substitution-notes) Variable substitution is applied for only the JSON keys predefined in the object hierarchy. It does not create new keys. – PatrickLu-MSFT Jan 31 '20 at 10:53
  • @NitinJain 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. Also updated my reply. – PatrickLu-MSFT Jan 31 '20 at 10:55
  • Replacing whole **appseting.json** with new one is not full filling my needs. I moved with **PS script** written by me to add new elements in Arrays of Appsettings.json – Nitin Jain Feb 06 '20 at 13:06
  • 1
    @NitinJain Thanks for your kindly response. Glad to hear this. If my reply helped or gave a right direction. Appreciate for marking it as an answer which will also help others in the community. Besides, you could also choose to share your PS script here to help those people which get stuck with same problem as you. – PatrickLu-MSFT Feb 06 '20 at 14:30