简体   繁体   English

如何使用 PowerShell 将新的 JsonArray 属性添加到现有的 Json 属性

[英]How to Add a new JsonArray Property to a Existing Json Property using PowerShell

I already have a Json value got from calling an API, I want to add a new Json List to the exiting Property, So我已经有一个通过调用 API 获得的 Json 值,我想向现有属性添加一个新的 Json 列表,所以

 #calling some API in a loop Start

        $apijson = Invoke-WebRequest -Uri $api -ErrorAction SilentlyContinue | ConvertFrom-Json
        $response = $apijson| ConvertTo-Json 

        $null = $data.Add($response);

 #calling some API in a loop End

My apiJson will look like the below我的 apiJson 将如下所示

  {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": [
                {
                    "delegation": 1
                }
            ]
        }

Now I have a custom new JsonArray现在我有一个自定义的新 JsonArray

[
    {
        "name":  "TLE",
        "Strength":  128
    },
    {
        "name":  "TLS",
        "trength":  415
    }
]

I want to add the above JsonArray to my original Json Property which makes a full Json like below我想将上面的 JsonArray 添加到我原来的 Json 属性中,这使得完整的 Json 如下所示

{
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": [
                {
                    "delegation": 1
                }
            ]
        },
    "Strength": [
         {
            "name":  "TLE",
            "Strength":  128
          },
        {
           "name":  "TLS",
           "trength":  415
        }
       ]

I tried with Addmember, Concatination nothing working unfortunately.不幸的是,我尝试使用 Addmember,Concatination 没有任何效果。

This is for when you are adding Strength as a property to an existing single object (converted from JSON).这是用于将Strength作为属性添加到现有单个对象(从 JSON 转换)时。 When using Add-Member , you need to force your new object to be a single array object.使用Add-Member ,您需要强制您的新对象成为单个数组对象。

$apijson = Invoke-WebRequest -Uri $api -ErrorAction SilentlyContinue | ConvertFrom-Json
$newjson = @'
[
    {
        "name":  "TLE",
        "Strength":  128
    },
    {
        "name":  "TLS",
        "trength":  415
    }
]
'@ | ConvertFrom-Json
$apijson | Add-Member -Type NoteProperty -Name 'Strength' -Value @($newjson)
$apijson | ConvertTo-Json -Depth 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM