Click here to Skip to main content
15,942,804 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am getting error while trying to convert from JSON to Data table and mentioned below

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

JSON format is mentioned below
{
    "access_token": "b9fbb7994e50899e2d5eaf012eee9fb0.d3e8a8d4ba960e9aca4cc156",
    "api_domain": "http://www.codeproject.com",
    "token_type": "Bearer",
    "expires_in": 12312
}


What I have tried:

I have tried below mentioned code but still getting error

C#
private static DataTable ConvertJSONToDataTable(string JSONData)
        {
            DataTable dtEmpShiftData = new DataTable();
            try
            {


                dtEmpShiftData = (DataTable)JsonConvert.DeserializeObject(JSONData, (typeof(DataTable)));

                return dtEmpShiftData;
            }
            catch (Exception ex)
            {
                //logger.Error(ex);
                return dtEmpShiftData;
            }
        }

please check and suggest on this.
Posted
Updated 15-Dec-22 23:05pm
v2

Please have a look at Deserialize a DataSet[^]

As OriginaGriff already suggested you have to embed the raw data into square brackets.
You could achieve this by using the following method:
C#
private static string AddSquareBrackets(string json)
{
    return $"[{json}]";
}

C#
var json = @"
	{
		'access_token': 'b9fbb7994e50899e2d5eaf012eee9fb0.d3e8a8d4ba960e9aca4cc156',
		'api_domain': 'http://www.codeproject.com',
		'token_type': 'Bearer',
		'expires_in': 12312                
	}";

json = AddSquareBrackets(json);

var dt = ConvertJsonToDataTable(json);

private static DataTable ConvertJsonToDataTable(string jsonData)
{
	try
	{
		return JsonConvert.DeserializeObject<DataTable>(jsonData);
	}
	catch
	{
		return null;
	}
}

The handling ot the exception message here is left out for brevity.
In case of a failure you can return null.
You have to check then if the result is not null.
 
Share this answer
 
v3
Comments
Shankrayya R 26-Nov-20 23:39pm    
Thanks for the answer.. i am getting dynamic json data from the external source and do i have to add this additional in the Json String ?
Try using Newtonsoft JSON. Just add square brackets round your JSON, and it's one line of code:
[{
    "access_token": "b9fbb7994e50899e2d5eaf012eee9fb0.d3e8a8d4ba960e9aca4cc156",
    "api_domain": "http://www.codeproject.com",
    "token_type": "Bearer",
    "expires_in": 12312
}]

C#
DataTable table = JsonConvert.DeserializeObject<DataTable>(jsonString);
 
Share this answer
 
Comments
Shankrayya R 3-Dec-20 5:11am    
Thanks a lot i am able to convert json to data table
OriginalGriff 3-Dec-20 5:28am    
You're welcome!
datatable = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<classobject>(jsonstring);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900