How to Convert Json String into DataTable in ASP.Net with C#

Question

I am using this API. It gives json output according to user input. For Example:

{
"apple.com":{"status":"regthroughothers","classkey":"domcno"},
"asdfgqwx.com":{"status":"available","classkey":"domcno"},
"microsoft.org":{"status":"unknown"},
"apple.org":{"status":"unknown"},
"microsoft.com":{"status":"regthroughothers","classkey":"domcno"},
"asdfgqwx.org":{"status":"unknown"}
}

Now I want to change this json output in dataTable and then Bind in Data Control like Gridview and repeater.

I am using this method to do this but when I passed above json output in this method , It throws an Error

Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.

Solution

To convert to DataTable You must understood what You are doing:

DataTable represents a collection of rows and columns your JSON string must represent a collection which can by convert to collection of rows and columns.

This is example of correct JSON file which can by change to DataTable:

[{
    "column1": "1788",
    "column2": "19"
},
{
    "column1": "1789",
    "column2": "24"
},
{
    "column1": "1790",
    "column2": "24"
},
{
    "column1": "1790",
    "column2": "23"
},
{
    "column1": "1790",
    "column2": "21"
}]

Each pair column1 and column2 is the row

Now Your JSON is not good. I change it to this: (correct DataTable schema)

  [{
        "name": "apple.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.com",
        "status": "available",
        "classkey": "domcno"
    },
    {
        "name": "microsoft.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "apple.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "microsoft.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.org",
        "status": "unknown",
        "classkey": "domcno"
    }]

And I add [ sign and ] sign at the beginning and the end of array

Next You can do this. It is working example for deserializing above JSON string to DataTable

using Newtonsoft.Json;

public class JsonExample
    {

        private string jsonObject = "[{ \"name\": \"apple.com\",    \"status\": \"regthroughothers\",   \"classkey\": \"domcno\"},{ \"name\": \"asdfgqwx.com\", \"status\": \"available\",  \"classkey\": \"domcno\"},{ \"name\": \"microsoft.org\",    \"status\": \"unknown\",    \"classkey\": \"\"},{   \"name\": \"apple.org\",    \"status\": \"unknown\",    \"classkey\": \"\"},{   \"name\": \"microsoft.com\",    \"status\": \"regthroughothers\",   \"classkey\": \"domcno\"},{ \"name\": \"asdfgqwx.org\", \"status\": \"unknown\",    \"classkey\": \"domcno\"}]".Trim();

        public JsonExample()
        { 
            DataTable items = JsonConvert.DeserializeObject<DataTable>(jsonObject);

            foreach (DataRow item in items.Rows)
            {
                Console.WriteLine($"Name: {item[0]} Status: {item[1]}  classkey {item[2]} " );
            }

        }
    }

BUT if You sill don;t want to change JSON file

private string jsonObject = JSON_String.Replace("{", "[{").Replace("}", "}]");

public JsonExample()
        {
            JArray jArray = JArray.Parse(jsonObject);

            DataTable dt = new DataTable();

            dt.Columns.Add("Name");
            dt.Columns.Add("status");
            dt.Columns.Add("classkey");

            foreach (JProperty item in jArray[0])
            {
                var jArray2 = JArray.Parse(item.Value.ToString());

                foreach (var item2 in jArray2)
                {
                    dt.Rows.Add(item.Name, item2["status"], item2["classkey"]);
                    Console.WriteLine($"Name: {item.Name} Status: {item2["status"]}  classkey {item2["classkey"]} ");
                }
            } 
        }

Effect is the same as but You don't need do change the JSON String exept 2 replace.

Effect when You parse first string with method I write:

enter image description here

Additional information:

Convert JSON response to DataTable