title: Deserialize JSON to DataTable with JSON.Net
body: <p>I am trying to deserialize a JSON payload into a datatable using JSON.Net for .Net 3.5. My JSON string is in the format:</p>
  
  <pre><code>{"d":[{"__metadata":{"id":"http://testingurl.mydomain.net/Mysvc.svc/AccountingDate(datetime'2013-08-02T00%3A00%3A00')","uri":"http://testingurl.mydomain.net/Mysvc/AccountingDate(datetime'2013-08-02T00%3A00%3A00')","type":"AccountingDate"},"ACCOUNTING_DT":"\/Date(1375401600000)\/","DTTM_STAMP":"\/Date(1374278400000)\/","PRE_ACCOUNTING_DT":"\/Date(1374883200000)\/","FISCAL_YEAR":"2014","ACCOUNTING_PERIOD":"6","FYR_BEGIN_DT":"\/Date(1372464000000)\/","FYR_END_DT":"\/Date(1403827200000)\/","FMN_BEGIN_DT":"\/Date(1375488000000)\/","FMN_END_DT":"\/Date(1377820800000)\/","FISCAL_MONTH":"2","FISCAL_WEEK":"F","WK_BEGIN_DT":"\/Date(1374969600000)\/","WK_END_DT":"\/Date(1375488000000)\/","CALENDAR_ID":"WE","SETID":"EYLLP","COMPANY_DESCR":"Ernst &amp; Young LLP","PRIOR_FYR_END_DT":"\/Date(1372377600000)\/","PRIOR_FYR_TODATE":"\/Date(1343347200000)\/","CAL_MONTH":"8","IW_CREATE_DT":"\/Date(1121385600000)\/","IW_UPDATE_DT":"\/Date(1374883200000)\/","INSTANCE_ID":"PA01","PMTD_BEGIN_DT":"\/Date(1372464000000)\/","PMTD_END_DT":"\/Date(1375401600000)\/","MTD12_BEGIN_DT":"\/Date(1343433600000)\/","MTD12_END_DT":"\/Date(1374796800000)\/","PRIOR_FYR_BEGIN_DT":"\/Date(1341014400000)\/","CALENDAR_MONTH_NAME":"August","CALENDAR_YEAR":"2013","PRY_FMN_BEGIN_DT":"\/Date(1341014400000)\/","PRY_FMN_END_DT":"\/Date(1343952000000)\/"}]}
  </code></pre>
  
  <p>I have validated this string with multiple online parsers and they say it is valid. 
  I have tried deserialzing with the following methods.</p>
  
  <p>Method 1: <code>jsonResponse</code> is equal to the string above</p>
  
  <pre><code>JSONNet.JsonSerializer json = new JSONNet.JsonSerializer();
  json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
  json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
  json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
  StringReader sr = new StringReader(jsonResponse);
  JSONNet.JsonTextReader treader = new JSONNet.JsonTextReader(sr);
  DataTable dt = json.Deserialize(treader, (typeof(DataTable))) as DataTable;
  </code></pre>
  
  <p>Method 2: <code>jsonResponse</code> is equal to the string above</p>
  
  <pre><code>DataSet ds = JSONNet.JsonConvert.DeserializeObject&lt;DataSet&gt;(jsonResponse);
  </code></pre>
  
  <p>Both methods have issues.</p>
  
  <ul>
  <li>Method 1 returns a datatable that has no columns or rows</li>
  <li><p>Method 2 throws an error:</p>
  
  <blockquote>
    <p>Specified argument was out of the range of valid values.</p>
  </blockquote>
  
  <p>The stacktrace is as follows:</p>
  
  <blockquote>
    <p>at Newtonsoft.Json.Converters.DataTableConverter.GetColumnDataType(JsonToken tokenType)<br>
    at Newtonsoft.Json.Converters.DataTableConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)<br>
    at Newtonsoft.Json.Converters.DataSetConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)<br>
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)<br>
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)<br>
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)<br>
    at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)<br>
    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)<br>
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)<br>
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)</p>
  </blockquote></li>
  </ul>
  
  <p>I believe the <code>JsonToken</code> being passed to the <code>GetColumnDataType</code> call at the top of the trace is the <code>StartObject</code>.</p>