title: How can I derive a C# dynamic object from a web api JSON reponse?
body: <p>Most of the time we don't, by default, have defined classes in our static language of choice, which presents white a quandary over how to deserialize the e.g. JSON returned from a call to a public and open wen api, dedicated to no single language, except the "language" of e.g. HTML and JSON.</p>
  
  <p>In .NET, these days, nearly all api queries are done with <code>HttpClient</code>, something like the Google Books API ISBN query below:</p>
  
  <pre><code>public class GoogleBooksClient
  {
      private const string IsbnUrl = "books/v1/volumes?q=isbn:{0}";
      private static HttpClient _client = new HttpClient();
      ...
      public async Task&lt;object&gt; GetDetailsByIsbn(string isbn)
      {
          var json = await _client.GetStringAsync(string.Format(IsbnUrl, isbn));
          dynamic objX = JsonConvert.DeserializeObject(json);
          return objX;
      }
  }
  </code></pre>
  
  <p>The biggest problem here is that whether <code>objX</code> is declared <code>var</code>', <code>object</code>, or <code>dynamic</code>, it is always a reference to a big, ugly <code>JObject</code> instance when <code>DeserializeObject</code> is called without a known type. In this case, the JSON object returned is incredibly complex, and there be dragons waiting for those who endeavour to write a C# class that the said JSON can be parsed into.</p>
  
  <p>This is an ideal, and intended, opportunity for the use of a C# <code>dynamic</code> object, where as the JSON is parsed, properties (and rarely functions for API responses) can be recursively added to the said dynamic object.</p>
  
  <p>Instead, NewtonSoft.JSON tightly binds the data held by the JSON with the heavy red-tape ligature of a quite opaque <code>Jobject</code> data structure. I'm surprised and disappointed that having come so far, NewtonSoft cannot simply extract a pure dynamic object, without all the obfuscating bureaucracy of their <code>JObject</code> maze.</p>
  
  <p>Is there no other way to simply parse the JSON into a C# dynamic object, just like it would be parsed into an explicitly dynamic JavaScript object in other scenarios? </p>
  
  <p><strong>ADDED:</strong> By "<em>obfuscating bureaucracy of their JObject maze</em>", I mean the following code:</p>
  
  <pre><code>var json = JsonConvert.SerializeObject(new Person {Id = 196912135012878, Age = 47, Name = "Brady"});
  var jObj = JsonConvert.DeserializeObject(json);
  </code></pre>
  
  <p>produces a <code>JObject</code> instance that my debugger shows as:</p>
  
  <p><a href="https://i.stack.imgur.com/q685W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q685W.png" alt="Debugger view of JObject properties"></a></p>
  
  <p>This looks, to me, much more like something that should be used internally during parsing of the JSON, and not an end product of that parsing, which should be a pure representation of only the properties parsed from that JSON. That would ideally be a C# dynamic object with no properties for use by any parser.</p>