powershell json object with square brackets
<p>I have an import of 1+ emails that have to be added to a json object. A foreach loop checks to see if the emails are valid (code not addded to this example). When the import contains 2+ cases, the format of the json object is correct:</p>

<pre><code>[
{
    "Name":  "John Doe",
    "Email":  "email@gmail.com"
},
{
    "Name":  "Jane Doe",
    "Email":  "email@live.com"
}
]
</code></pre>

<p>Namely, square brackets with each object in curly brackets, separated by comma.
But when the foreach loop only returns 1 valid email, the format becomes:</p>

<pre><code>{
    "Name":  "John Doe",
    "Email":  "email@gmail.com"
}
</code></pre>

<p>This is the code I am running:</p>

<pre><code>        $body = foreach($row in $mailinglistimp) 
                             {                             
                                 @{
                                 Email=$row.EPOSTADR 
                                 Name=$row.KUNDE_NM
                                 }
                              }
        $body_json = ConvertTo-json $body
</code></pre>

<p>How can I force the object to look like this with only one item in the foreach loop?</p>

<pre><code>[
{
    "Name":  "John Doe",
    "Email":  "email@gmail.com"
},
]
</code></pre>

<p>On a related note, a similar problem was solved by using [int[]] $ids inside another json object, but I cant get a similar code to work using [string[]].</p>