title: Trying to Deserialize JSON to typed Object in Powershell
body: <p>I'm trying to deserialize a JSON string (which is coming from an API response) into my typed objects in Powershell, however, I keep receiving two types of errors.</p>
  <p>I have two PowerShell classes such as:</p>
  <pre><code>cpublic class DataType
  {
      [string] key
      [string] dataName
  }
  
  public class DataTypeList
  {
      [string] key 
      [string] name 
      [string] localeKey 
  }
  
  public class Event
  {
      [string] key
      [string] name
      [string] localeKey
      [DataType] DataType
      [List[DataTypeList]] DataTypeList
  }
  
  public class Root
  {
     [List[Event]] $events
  }
  </code></pre>
  <p>and my JSON response looks something like this:</p>
  <pre><code>  {
     &quot;Events&quot;:[
        {
           &quot;key&quot;:&quot;1234&quot;,
           &quot;name&quot;:&quot;Bob Muprhy&quot;,
           &quot;localeKey&quot;:&quot;4&quot;,
           &quot;DataType&quot;:{
              &quot;key&quot;:&quot;1111111&quot;,
              &quot;dataName&quot;:&quot;Name One&quot;
           },
           &quot;DataTypeList&quot;:[
              {
                 &quot;key&quot;:&quot;983984&quot;,
                 &quot;name&quot;:&quot;New name&quot;,
                 &quot;localeKey&quot;:&quot;34985&quot;
              },
              {
                 &quot;key&quot;:&quot;124543534&quot;,
                 &quot;name&quot;:&quot;New name new Name&quot;,
                 &quot;localeKey&quot;:&quot;asdfsadf&quot;
              }
           ]
        },
        {
           &quot;key&quot;:&quot;123456567&quot;,
           &quot;name&quot;:&quot;Pete big&quot;,
           &quot;localeKey&quot;:&quot;4&quot;,
           &quot;DataType&quot;:{
              &quot;key&quot;:&quot;1111111&quot;,
              &quot;dataName&quot;:&quot;Name 1&quot;
           },
           &quot;DataTypeList&quot;:[
              {
                 &quot;key&quot;:&quot;983984&quot;,
                 &quot;name&quot;:&quot;New name&quot;,
                 &quot;localeKey&quot;:&quot;34985&quot;
              },
              {
                 &quot;key&quot;:&quot;124543534&quot;,
                 &quot;name&quot;:&quot;New name new Name&quot;,
                 &quot;localeKey&quot;:&quot;asdfsadf&quot;
              }
           ]
        }
     ]
  }
  </code></pre>
  <p>My code currently looks something like this, I'm using Invoke-RestMethod as I've read up that it will automatically apply the ConvertFrom-JSON to the API response/content</p>
  <p>Here i am getting the json response:</p>
  <pre><code>$json = Invoke-RestMethod -Uri &quot;&lt;API Link&gt;&quot;
  </code></pre>
  <p>Here I am trying to &quot;deserialize the JSON into my object&quot; by using the Cast-Initialization technique</p>
  <pre><code>$response = [Root]($json)
  </code></pre>
  <p>However i will get an error response saying: <em>Cannot create object of type &quot;Root&quot;. Cannot convert the &quot;System.Object[]&quot; value of type &quot;System.Object[]&quot; to type &quot;System.Collections.Generic.List`1[FeaturedEvent]</em></p>
  <p>I tried also to do something like:</p>
  <pre><code>$response = [Root]($json.Events)
  </code></pre>
  <p>However, that will also return error <em>Cannot convert the &quot;System.Object[]&quot; value of type &quot;System.Object[]&quot; to type &quot;Root</em></p>
  <p>When deserializing something in C# i know I would be able to do something like</p>
  <pre><code>*JsonConvert.DeserializeObject&lt;Root&gt;(json);*
  </code></pre>
  <p>Trying to replica the same here in Powershell</p>
  <p>I also did try this:</p>
  <pre><code> $root = [Root]::new()
  
   $root.Events = $json.Events
  </code></pre>
  <p>However again I received the same error</p>