title: Deserialize JSON to a dynamic object or a class in C#
body: <p>I am trying to deserialize JSON into an object so I can add it to elastic search. JSON can be of many different object types in the project so I would like the function to be dynamic.</p>
  <p>First I am serializing the Data that I get from EF Core context</p>
  <pre><code>var serializedObject = JsonConvert.SerializeObject(document, Formatting.None,
                         new JsonSerializerSettings()
                         {
                             ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                         });
  </code></pre>
  <p>Next I would like to deserialize to an object. For example if I have</p>
  <pre><code>public class EValues 
  {
      public dynamic values { get; set; }
  }
  
  var test =  JsonConvert.DeserializeObject&lt;EValues&gt;(serializedObject.ToString());
  </code></pre>
  <p>I would like the JSON to be deserialized to the below:</p>
  <pre><code>{
     &quot;values&quot;:{
        &quot;StudentId&quot;:&quot;60712555-ff1d-4a3e-8c81-08d9c2fc4423&quot;,
        &quot;Student&quot;:{
           &quot;Name&quot;:&quot;string&quot;,
           &quot;Country&quot;:&quot;string&quot;,
           &quot;Street&quot;:&quot;string&quot;
        }
     }
  }
  </code></pre>
  <p>The <code>serializedObject</code> JSON I am actually trying to deserialize:</p>
  <pre><code>{
     &quot;StudentId&quot;:&quot;60712555-ff1d-4a3e-8c81-08d9c2fc4423&quot;,
     &quot;Student&quot;:{
        &quot;Name&quot;:&quot;string&quot;,
        &quot;Country&quot;:&quot;string&quot;,
        &quot;Street&quot;:&quot;string&quot;
     }
  }
  </code></pre>