0

I was trying to append multiple Json object in a single JSON Array using Powershell

From this answer I got the solution

$users = [System.Collections.ArrayList]::new();

 #calling some API in a loop Start

            $response = $json | ConvertTo-Json 
            $null = $data.Add($response);

 #calling some API in a loop End

    $result = @{ Data = $data};


    $result | ConvertTo-Json  | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File 'C:\Jay\inv\$filename.json'

For an instance Lets say the JSON Response from the API is like

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

Then as per the script my final file i was expecting like

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

But I'm getting additional quotation marks like below which makes the error in the json

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

Could someone help to find what is going wrong with the powershell script ?

Update 1

$result = @{ Data = $data | ConvertFrom-Json };

I'm getting the json like

{
    "Data" : [
        {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": "     "
            ]
        },
        {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": "     "
            ]
        }
    ]
}
5
  • 1
    @{ Data = $data}; -> @{ Data = $data |ConvertFrom-Json}; Commented Mar 26, 2020 at 12:26
  • 1
    The variable name $Json is confusing (why would you do $json | ConvertTo-Json if it is already json?). Anyways, you probable do not want to convert the object to Json at all at this level. Meaning just: $null = $data.Add($json) Commented Mar 26, 2020 at 12:34
  • @MathiasR.Jessen thanks I just did the same as you mentioned , now i get rid of the quotation marks, however facing another issues, The endpoint (json array ) is coming as empty json and not array, please see my update 1 in the above question Commented Mar 26, 2020 at 13:11
  • Add -Depth 4 to ConvertTo-Json Commented Mar 26, 2020 at 13:14
  • @MathiasR.Jessen Thanks a lot please provide your answer, I'll accept it :) Commented Mar 26, 2020 at 13:33

0

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.