title: Convert JSON to Datatable c#
body: <p>I am trying to convert json string to Datatable in WEBAPI</p>
  
  <pre><code>The json string looks like
  [
  [
      "Test123",
      "TestHub",
      "TestVersion",
      "TestMKT",
      "TestCAP",
      "TestRegion",
      "TestAssembly",
      "TestProduct",
      "Testgroup",
      "Testsample",
      "1806",
      "1807",
      "1808",
      "1809",
      "1810",
      "1811",
      "1812",
      "1901",
      "1902",
      "1903",
      "1904",
      "1905",
      "1906",
      "1907",
      "1908",
      "1909",
      "1910",
      "1911",
      "1912"
  ],
  [
      "Sample12",
      "Sample879",
      "201806.1.0",
      "Sample098",
      "TSA CBU",
      "B8",
      "B8",
      "63",
      "63EM",
      "EM 42 T",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0"
  ],
  [
      "Sample121233",
      "Sample233879",
      "2012323806.1.0",
      "Sampl233e098",
      "TSA CBU",
      "B8",
      "B8",
      "B3",
      "B3ULUE",
      "UL 42 R",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0",
      "0"
  ]
  ]
  </code></pre>
  
  <p>WEBAPI method where i am posting the json i want to convert the json into datatable with some default column names..................................................................................</p>
  
  <pre><code>public async Task&lt;HttpResponseMessage&gt; Uploadjson(HttpRequestMessage request)
      {
          try
          {
              var jsonString = await request.Content.ReadAsStringAsync();
              var jArrayObj = JsonConvert.DeserializeObject&lt;JArray&gt;(jsonString);
              JArray jsonArray = JArray.Parse(jsonString) as JArray;
  
             DataTable dt = JsonToDataTable(jsonArray[0].ToString());
  
          }
          catch (Exception exception)
          {
              Log.Info("EndPoint Out Time :" + DateTime.Now.ToString());
          }
  
          return new HttpResponseMessage(HttpStatusCode.OK);
      }
  </code></pre>
  
  <p>And here is my JsonToDataTable method</p>
  
  <pre><code>    public static DataTable JsonToDataTable(string json)
      {
          var jsonLinq = JObject.Parse(json);
  
          // Find the first array using Linq
          var srcArray = jsonLinq.Descendants().Where(d =&gt; d is JArray).First();
          var trgArray = new JArray();
          foreach (JObject row in srcArray.Children&lt;JObject&gt;())
          {
              var cleanRow = new JObject();
              foreach (JProperty column in row.Properties())
              {
                  // Only include JValue types
                  if (column.Value is JValue)
                  {
                      cleanRow.Add(column.Name, column.Value);
                  }
              }
              trgArray.Add(cleanRow);
          }
  
          return JsonConvert.DeserializeObject&lt;DataTable&gt;(trgArray.ToString());
  
          //bool columnsCreated = false;
          //DataTable dt = new DataTable(tableName);
  
          //Newtonsoft.Json.Linq.JObject root = Newtonsoft.Json.Linq.JObject.Parse(json);
          //Newtonsoft.Json.Linq.JArray items = root(tableName);
  
          //Newtonsoft.Json.Linq.JObject item = default(Newtonsoft.Json.Linq.JObject);
          //Newtonsoft.Json.Linq.JToken jtoken = default(Newtonsoft.Json.Linq.JToken);
  
          //for (int i = 0; i &lt;= items.Count - 1; i++)
          //{
          //    // Create the columns once
          //    if (columnsCreated == false)
          //    {
          //        item = (Newtonsoft.Json.Linq.JObject) items(i);
          //        jtoken = item.First;
  
          //        while (jtoken != null)
          //        {
          //            dt.Columns.Add(new DataColumn(((Newtonsoft.Json.Linq.JProperty)jtoken).Name.ToString()));
          //            jtoken = jtoken.Next;
          //        }
  
          //        columnsCreated = true;
          //    }
  
          //    // Add each of the columns into a new row then put that new row into the DataTable
          //    item = (Newtonsoft.Json.Linq.JObject)items(i);
          //    jtoken = item.First;
  
          //    // Create the new row, put the values into the columns then add the row to the DataTable
          //    DataRow dr = dt.NewRow;
  
          //    while (jtoken != null)
          //    {
          //        dr(((Newtonsoft.Json.Linq.JProperty)jtoken).Name.ToString()) = ((Newtonsoft.Json.Linq.JProperty)jtoken).Value.ToString();
          //        jtoken = jtoken.Next;
          //    }
  
          //    dt.Rows.Add(dr);
          //}
  
          //return dt;
  
      }
  </code></pre>
  
  <p>At var jsonLinq = JObject.Parse(json); I am getting error </p>
  
  <pre><code>  Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
  </code></pre>