json - 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).
解决方案
[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
推荐阅读
- vue.js - 使用 vue getter/setter 指向 localStorage 时的奇怪行为
- excel - 每秒自动刷新excel中的外部文本文件
- c++ - 我可以在 C++ 中 typedef 未命名的结构/类吗?
- c# - 使用从 MySQL 数据库表中选择的值行填充 ASP.Net C# 表单中的“asp:Panel”
- java - 如何比较两种不同大小的 List Objects Java
- java - 为什么将所有 JavaFX 依赖项放在类路径中而不用担心 Java 模块是不够的?
- angular - 从联系人获取所有号码的离子联系人问题
- javascript - 在 MongoDB 中查询多个集合时需要一些帮助(排序、搜索)
- angular - 如何告诉 p-table 使用子字段对某些列进行排序
- google-ads-api - google ads API,CONTAINS ANY 不是在 WHERE 子句中与“ad_group.name”一起使用的有效运算符