title: Powershell - Invoke-RestMethod - POST nested JSON
body: <p>I'm trying to interact with an API to POST some data to a service we use, in order to add some email addresses to a policy. This will eventually take a list of email addresses and loop through it for each one, but for now I'm just trying to get it to work with a single address.</p>
  <p>I'm using Powershell 7, I would say I'm intermediate with powershell, but this is my first foray into interacting with an API, using JSON, and using the Invoke-RESTMethod commandlet. The API reference for the service shows lots of code examples in other languages, just not PS!</p>
  <p>Here's the problem I'm running in to. I'm trying to formulate a -Body statement that looks to be 3 elements instead of two. Here is what that example statement looks like in CURL:</p>
  <pre><code>{
    &quot;custodians&quot;: [
      {
        &quot;emailId&quot;: &quot;john.doe@someorg.com&quot;
      }
    ],
    &quot;action&quot;: &quot;add&quot;
  }
  </code></pre>
  <p>The Action:Add part is fine, easy peasy. I'm trying to figure out how to correctly format the custodians part. I cant figure out how to do 3 elements instead of two. I've tried different combinations of {} [] and () to no avail:</p>
  <pre><code>&quot;custodians&quot; = ({&quot;emailId&quot; = &quot;emailaddress@place.com&quot;})
  </code></pre>
  <p>gives me an &quot;The assignment expression is not valid&quot; error</p>
  <pre><code>&quot;custodians&quot; = [{&quot;emailId&quot; = &quot;emailaddress@place.com&quot;}]
  </code></pre>
  <p>and</p>
  <pre><code>&quot;custodians&quot; = [@{&quot;emailId&quot; = &quot;emailaddress@place.com&quot;}]
  </code></pre>
  <p>give me an &quot;Missing type name after '['.&quot; error.</p>
  <p>These, as well as a few other combinations I've typed out, all showed formatting errors in VSCode so I already knew they wouldn't work, I'm just not sure why. I'm sure I just haven't cracked the right combination of @, {}, [] or () but I cant seem to find any info online (probably because I'm not using the right vocabulary or phrasing in my searches) that shows how to format with three elements.</p>
  <p>If its relevant or helpful, here is a larger code sample of the whole query Im working on. Assume the Auth headers are fine (I can request my auth tokens and do GETs without issue so far):</p>
  <pre><code>$headers = @{
      &quot;Authorization&quot; = $bearerAuthValue
      &quot;Accept&quot; = &quot;application/json&quot;
      &quot;Content Type&quot; = &quot;application/json&quot;
  } 
  
  $body = @{
      &quot;custodians&quot; = @(emailId = &quot;emailaddress@place.com&quot;),
      &quot;Action&quot; = &quot;add&quot;
  }
  $uri = &quot;https://thisisanaddress.com&quot;
  Invoke-RestMethod -Method 'POST' -Uri $uri -Headers $headers -Body $body
  </code></pre>