title: Variable expansion in JSON object for use with Invoke-RestMethod
body: <p>Over the past few days I've been looking for documentation on variable expansion in JSON objects. I've tried a lot of recommendations and fixes, but none of them seem to work quite as expected. I'm left wondering if there's a better way than I have implemented. Anyway, on to my use case.</p>
  
  <p><strong>Use Case</strong>
  I have an array of integers, which represent objects in a web application. These objects are able to have a relationship established between them by making a POST request to an API endpoint.</p>
  
  <p>This array of integers is referred to as <code>$combined</code>.</p>
  
  <pre class="lang-bsh prettyprint-override"><code>PS C:\&gt; $combined
  
          i_id       m_id
  ------------ ----------
         75419       6403
         75420       6403
         75421       6403
         75410       6403
  </code></pre>
  
  <p>To set up the parameters used for my POST request, I've created another array called <code>$associationParams</code>.</p>
  
  <pre class="lang-bsh prettyprint-override"><code>    $associationParams = @{
          ContentType = "application/json"
          Body = '{"id": '+$($thing).m_id+'}'
          Method = "POST"
          URI = "https://application/api/in/$($thing.i_id)/endpoint"
          Headers = $headers
      }
  </code></pre>
  
  <p>The <code>$headers</code> variable above is the authorization token.</p>
  
  <p>So, my initial inclination is to loop through the array of integers (<code>$combined</code>) and call <code>Invoke-RestMethod</code> as so:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>    Foreach ($thing in $combined) {
          Invoke-RestMethod @iocAssociationParams
      }
  </code></pre>
  
  <p><strong>Problem</strong>
  So, I don't really have a problem per se... There seems to be some inconsistency in my testing where the final <code>ForEach</code> doesn't seem to loop through all the elements in the <code>$combined</code> array... But I am wondering if there's a better way. I've tried string formatting the JSON object in the Body parameter in <code>$associationParams</code>, but it definitely didn't work. I've also tried various forms of quoting, but they didn't work either. Is trying to splat the parameters for <code>Invoke-RestMethod</code> the best way? Should I just list out all the parameters for <code>Invoke-RestMethod</code> in the <code>ForEach</code> loop? </p>