title: How do I correctly convert a HashTable to JSON in PowerShell?
body: <p>I'm using PowerShell to send a <code>POST</code> request to a <code>REST API</code>. The body of the request looks like this: </p>
  
  <pre><code>{
    "title": "game result",
    "attachments": [{
        "image_url": "http://contoso/",
        "title": "good work!"
      },
      {
        "fields": [{
            "title": "score",
            "value": "100"
          },
          {
            "title": "bonus",
            "value": "50"
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>Now, the following PowerShell script produces the wrong output:</p>
  
  <pre><code>$fields = @(@{title='score'; value='100'},@{title='bonus'; value='10'})
  $fieldsWrap = @{fields=$fields}
  #$fieldsWrap | ConvertTo-Json
  $attachments = @(@{title='good work!';image_url='http://contoso'},$fieldsWrap)
  $body = @{title='game results';attachments=$attachments}
  $json = $body | ConvertTo-Json
  $json 
  </code></pre>
  
  <p>Line 3 (if uncommented) produces the correct output, however line 7 produces:</p>
  
  <pre><code>{
    "attachments": [{
        "image_url": "http://contoso",
        "title": "good work!"
      },
      {
        "fields": "System.Collections.Hashtable System.Collections.Hashtable"
      }
    ],
    "title": "game result"
  }
  </code></pre>
  
  <p>It obviously writes out the type name of the <code>HashTable</code>, which is the default <code>ToString()</code> implementation I assume.<br>
  How do I get the correct output?</p>