title: Using PowerShell to process a JSON string
body: <p>Suppose I have a piece of JSON in a PowerShell string, e.g.</p>
  
  <pre><code>$s = '{"foo":"hello"}'
  </code></pre>
  
  <p>My goal is to turn this into an object that I can manipulate (e.g. changing/adding properties), and then convert back to a json string.</p>
  
  <p>So trying the obvious, I write:</p>
  
  <pre><code>$o = $s | ConvertFrom-Json    # convert the json string to an object
  $o.foo = "hello2"             # change an existing prop
  $o.bar = "World"              # add a new prop
  $s2 = $o | ConvertTo-Json     # convert back into a json string
  </code></pre>
  
  <p>The problem is that the object I get back from <code>ConvertFrom-Json</code> is of type <code>PSCustomObject</code>, which doesn't allow adding properties. So the 3rd line blows up with:</p>
  
  <p><code>
  Exception setting "bar": "The property 'bar' cannot be found on this object. Verify that the property exists and can be set."
  At line:1 char:1
  </code></p>
  
  <p><strong>Question:</strong> what is the best way to approach this without bringing too much complexity?</p>