Convert JSON to Datatable c#

convert nested json to datatable c#
convert json to datatable c# without newtonsoft
convert json to datatable c# using newtonsoft
convert json to datatable c# online
convert dynamic json to datatable c#
convert json to datatable javascript
convert jobject to datatable c#
json to datatable jquery

I am trying to convert json string to Datatable in WEBAPI

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"
]
]

WEBAPI method where i am posting the json i want to convert the json into datatable with some default column names..................................................................................

public async Task<HttpResponseMessage> Uploadjson(HttpRequestMessage request)
    {
        try
        {
            var jsonString = await request.Content.ReadAsStringAsync();
            var jArrayObj = JsonConvert.DeserializeObject<JArray>(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);
    }

And here is my JsonToDataTable method

    public static DataTable JsonToDataTable(string json)
    {
        var jsonLinq = JObject.Parse(json);

        // Find the first array using Linq
        var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
        var trgArray = new JArray();
        foreach (JObject row in srcArray.Children<JObject>())
        {
            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<DataTable>(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 <= 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;

    }

At var jsonLinq = JObject.Parse(json); I am getting error

  Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

you cant parse array with JObject you can read more about it here

but you can parse it to Jarray

JArray jsonArray = JArray.Parse(json);
foreach (JArray arrayRow in jsonArray)
        {
            foreach (JToken arrayItem in arrayRow))
            {
                //do stuff here
            }
        }

Converting json string to datatable, Start here: c# - Convert JSON to DataTable - Stack Overflow[^] and/or here: Json to DataSet/DataTable[^] Recommended method: Hide Copy� In the following example, I will discuss a problem statement of how to convert JSON to datatable in C#.As a solution, I will discuss three different ways from which we could Parse JSON to DataTable in C#.

It may be too late to answer this OP question. But this solution may help others in case if they run into similar situation

With Cinchoo ETL - an open source library, you can convert this json to DataTable easily

using (var r = ChoJSONReader.LoadText(json))
{
    DataTable dt = r.Select(rec => ((object[])rec.Value).ToDictionary()).AsDataTable();
}

How to Convert Json to Datatable in c#, How to Convert Json to Datatable in c#. Feb 27 2019 6:16 AM. [{"TypeId":"1"," TypeName":"Wastage not collected"},{"TypeId":"2","TypeName":"Drainage not� I have a json from text file and want to convert it into data table in C#. Here, I used dynamic object to get Deserialize Object from Newtonsoft.Json and processed accordingly to convert it into DataTable. Using the Code. The json.txt contains:

If its an valid JSON you can try to use Newtonsoft.Json then you can parse the JSON like this:

var jsonAsObject = JsonConvert.DeserializeObject(json);

If you have an model for your JSON you can convert it like this:

ModelName model = (ModelName)JsonConvert.DeserializeObject(json, typeof(ModelName));

Convert JSON String to DataTable in ASP.Net, Now, convert the JSON string to DataTable using the newtonsoft DLL. Such as, you are read my previous article Convert DataTable To JSON String in ASP.Net. Here I will explain how to convert a JOSN String to DataTable using a written helper function (in C#) and Newtonsoft DLL.

How to Convert JSON to Datatable in C#, As a solution, I will discuss three different ways from which we could Parse JSON to DataTable in C#. What is JSON Schema? JSON stands for JavaScript Object� # Method 3: Convert DataTable to JSON using Json.Net DLL (Newtonsoft): Look : Now in this method, we are going to convert our C# datatable to JSON using newtonsoft DLL. For this first, we need to download Json.Net DLL. We can download it from Nuget.org and then import Newtonsoft.Json namespace in our page as shown in below code.

Convert JSON string to DataTable in ASP.Net using C# and VB.Net, Net using C# and VB.Net. The JSON string will be first downloaded from an API using WebClient class and then will be converted to DataTable� To read more about DataTable please refer DataTable C#. Modern data flow from web is transported in various format and JSON is one of the popular. In this article we will see how to deserialize a Json into DataTable. Prerequisites: You must have a valid Json (string,file, web response) DataTable structure must meet the Data Contract with JSON

4 ways to Convert JSON to DataTable in C# - asp.net, Solutions: Here as a solution I will discuss three different ways from which we could Convert JSON to DataTable in C#. 1. Using Simple method. The DOM provides random access to data in a JSON payload. The JSON elements that compose the payload can be accessed via the JsonElement type. The JsonElement type provides array and object enumerators along with APIs to convert JSON text to common .NET types. JsonDocument exposes a RootElement property.

Comments
  • It's not clear why you want to use DataTable at all here. You could deserialise that JSON directly to a List<List<string>> for example.
  • Also the error you are getting is because you are doing JObject.Parse instead of JArray,Parse, which is needless as you already have the JArray object in the previous method before you do ToString() on it!
  • @DavidG +1 Thanks a lot we tried with deserializing to List<List<string>> and it works like a charm.
  • They are already parsing the JSON successfully, this answer doesn't really add anything useful.