0

I already have a Json value got from calling an API, I want to add a new Json List to the exiting Property, So

 #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

  {
            "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

[
    {
        "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

{
            "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.

3
  • Do not try to manipulate the Json string, deserialize the json strings, concatenate them and serialize them again: $api = Invoke-WebRequest -Uri $api -ErrorAction SilentlyContinue | ConvertFrom-Json; $Strength = $JsonArray | ConvertFrom-Json; $api, $Strength | ConvertTo-Json -Depth 5 Commented Mar 26, 2020 at 15:56
  • Please, stop calling anything that is returned from ConvertFrom-Json Json (like $apijson) as it no longer Json but an general object. I suspect that you also confusing yourself in this matter... Commented Mar 26, 2020 at 16:02
  • 1
    @iRon thanks for the clarification now i understood what you are actually referring to. I just did the conversation and reconversion again and again unnecessary Commented Mar 27, 2020 at 5:09

1 Answer 1

2

This is for when you are adding Strength as a property to an existing single object (converted from JSON). When using Add-Member, you need to force your new object to be a single array object.

$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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.