在PowerShell中将一个JSON组合到另一个JSON对象

wkyowqbh  于 2023-03-31  发布在  Shell
关注(0)|答案(2)|浏览(139)

我有两个json文件,我想把一个json文件作为对象合并到另一个json文件中。
我的第一个C1 json喜欢:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "connections_TestValue_name": {
            "defaultValue": "TestValue",
            "type": "String"
        }
    },
    "variables": {},
    "resources": []
}

另一个C2 json如下:

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

                                           },
                       "customParameterValues":  {

     ...
     ...
}

现在,我需要将Json文件C2作为对象"resources": []插入到另一个json文件C1,如下所示:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "connections_TestValue_name": {
            "defaultValue": "TestValue",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
      {
       "kind":  "V2",
       "properties":  {
        ...
        ...
      }
  ]
}

注意:C1C2不是实际的文件,是通过其他脚本获取的。

kokeuurv

kokeuurv1#

方向:

$C1Data = ConvertFrom-Json $C1 -AsHashTable
$C2Data = ConvertFrom-Json $C2 -AsHashTable
$C1Data.resources = $C2Data
$C1Data |ConvertTo-Json -Depth 9
kjthegm6

kjthegm62#

由于我不是powershell的Maven,我在多次尝试和调试后找到了答案:

$C1= $C1 | ConvertFrom-Json
$C2= $C2 | ConvertFrom-Json

$C1.resources+= $C2

$C1New= $C1 | ConvertTo-Json -depth 100

Write-Host "$C1New"

相关问题