我有一个Json文件作为配置文件,如下所示:

{
    "managedApiConnections": {
        "Testoffice365_Connection_1": {
            "api": {
                "id": "/subscriptions/.../office365"
            },
            "authentication": {
                "type": "ManagedServiceIdentity"
            },
            "connection": {
                "id": "/subscriptions/.../Test_Connection_1"
            },
            "connectionRuntimeUrl": "https://.../office365/xxx3501"
        },
        "Testsql_Connection_1": {
            "api": {
                "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastasia/managedApis/sql"
            },
            "authentication": {
                "type": "ManagedServiceIdentity"
            },
            "connection": {
                "id": "/subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.Web/connections/sql_SIT"
            },
            "connectionProperties": {
                "authentication": {
                    "audience": "https://database.windows.net/",
                    "type": "ManagedServiceIdentity"
                }
            },
            "connectionRuntimeUrl": "https://8xxxx.02.common.logic-eastasia.azure-apihub.net/apim/sql/xxxx1b"
        }
    },
    "serviceProviderConnections": {
        "Testblob_Connection_1": {
            "displayName": "blobxxx",
            "parameterSetName": "ManagedServiceIdentity",
            "parameterValues": {
                "authProvider": {
                    "Type": "ManagedServiceIdentity"
                },
                "blobStorageEndpoint": "@appsetting('blob_url_test')"
            },
            "serviceProvider": {
                "id": "/serviceProviders/AzureBlob"
            }
        },
        "Testsql_Connection_2": {
            "displayName": "SQL_xxxx",
            "parameterSetName": "ManagedServiceIdentity",
            "parameterValues": {
                "authProvider": {
                    "Type": "ManagedServiceIdentity"
                },
                "databaseName": "xxxx ",
                "managedIdentityType": "SystemAssigned",
                "serverName": "xxxx.database.windows.net"
            },
            "serviceProvider": {
                "id": "/serviceProviders/sql"
            }
        }
    }
}

Now,由于自动化的需要,为了满足新的连接实例,我需要在这个配置文件中添加额外的配置信息,比如添加新的O365连接,我需要add using powershell to get the content from below json template then add it Before the node 100:

{
    "<APIConnectionName>_<DeployENV>": {
    "api": {
        "id": "/subscriptions/<subscriptionsID>/providers/Microsoft.Web/locations/eastasia/managedApis/office365"
    },
    "authentication": {
        "type": "ManagedServiceIdentity"
    },
    "connection": {
        "id": "/subscriptions/<subscriptionsID>/resourceGroups/<resourceGroups>/providers/Microsoft.Web/connections/<APIConnectionName>_<DeployENV>"
    },
    "connectionRuntimeUrl": "https://xxxx/office365/<connectionRuntimeUrl>"
    }
}

注意:将模板添加到Json文件时,不需要outermost braces.

如何使用Powershell脚本来实现它?

推荐答案

编辑:简化版本,因为我意识到数组也可以.

$BaseObj = $BaseJson | ConvertFrom-Json
$TemplateObj = $TemplateJson | ConvertFrom-Json

$BaseObj.managedApiConnections = $TemplateObj, $BaseObj.managedApiConnections | ForEach-Object { $_ }

$BaseObj | ConvertTo-Json -Depth 100

ORIGINAL:
Here: please note I did insert the contents of the template literally.

# if the JSON are hardcoded in the script
# just use only ConvertFrom-Json, if they aren't already converted somewhere else in the script
$BaseJson = Get-Content $BaseJsonFilePath
$TemplateJson = Get-Content $TemplateJsonFilePath

$BaseObj = $BaseJson | ConvertFrom-Json
$TemplateObj = $TemplateJson | ConvertFrom-Json

$NewManagedApiCollectionObj = [System.Collections.Generic.List[object]]::new()

$TemplateObj, $BaseObj.managedApiConnections | ForEach-Object { $NewManagedApiCollectionObj.Add($_) }

$BaseObj.managedApiConnections = $NewManagedApiCollectionObj

$BaseObj | ConvertTo-Json -Depth 100

Json相关问答推荐

使用jolt删除空对象

为什么terraform不缩小这个策略JSON?'

Python将Pandas转换为嵌套的JSON

解析SQL中的嵌套JSON

如何在改装Android中将ResponseBody转换为JSONObject

使用JQ将JSON输出转换为CSV复杂 struct

具有 (RegEx) 模式的 json-schema 中的枚举

打印与 JSON 和 PowerShell 中的模式匹配的子项的父项名称

使用 SwiftUI 在 API 调用中解码嵌套 JSON 响应时遇到问题

如何用 Xidel 正确读取这个 JSON 文件?

获取json中某个键的索引

为什么在我们有 json_encode 时使用 CJSON 编码

Angularjs访问本地json文件

苗条的 JSON 输出

Java JSON 序列化 - 最佳实践

Json.Net:用于自定义命名的 JsonSerializer-Attribute

如何使用 LWP 发出 JSON POST 请求?

在视图中将 .Net 对象转换为 JSON 对象

处理 HTTP 请求正文中的可选 JSON 字段

Laravel 5 控制器将 JSON 整数作为字符串发送