title: How to receive JSON data into Web API ApiController method?
body: <p>I'm writing a Web API ApiController with several PUT methods that receive JSON data.  The JSON is not deterministic <em>and hence cannot be hard-mapped to a custom C# object</em>, but needs to be received as Dictionaries/Sequences (Maps/Lists).</p>
  
  <p>I have tried using an IDictionary for the data parm of the PUT method in the controller, and this sort of works -- the data appears to be mapped from JSON to the dictionary.  However, it's necessary to declare the dictionary as <code>&lt;String,Object&gt;</code>, and there's no clear way to then retrieve the Object values as their appropriate types.  (I've found a few suggested kluges in my searching, but they are just that.)</p>
  
  <p>There is also a System.Json.JsonObject type which I finally managed to get loaded via NuGet, but when I use that the system does not appear to know how to map the data.</p>
  
  <p>How is this typically done?  How do you implement an ApiController method that receives generic JSON?</p>
  
  <p>I can see three basic approaches:</p>
  
  <ol>
  <li>Somehow make Dictionary/Sequence work with Object or some such.</li>
  <li>Make something like System.Json.JsonObject work, perhaps by swizzling the routing info.</li>
  <li>Receive the JSON as a byte array and then parse explicitly using one of the C# JSON toolkits available.</li>
  </ol>
  
  <p>(As to how dynamic the data is, JSON objects may have missing entries or extraneous entries, and in some cases a particular entry may be represented as either a single JSON value or a JSON array of values.  (Where "value" is JSON array, object, string, number, Boolean, or null.)  In general, except for the array/not array ambiguity, the relation between keys and value types is known.)</p>
  
  <p>(But I should note that this is a large project and I'll be receiving JSON strings from several other components by other authors.  Being able to examine the received type and assert that it's as expected would be quite useful, and may even be necessary from a security standpoint.)</p>
  
  <p>(I should add that I'm a relative novice with C# -- have only been working with it for about 6 months.)</p>