Unexpected JSON token when reading DataTable

convert json to datatable c# newtonsoft
unexpected end when reading json. path
convert complex json to datatable c#
how to convert a json data from web api to a datatable
jsonconvert datatable
json to dataset c#
convert httpresponse to datatable c#
json to datatable vb net

I have the following json string:

{
    "Orders": [{
        "SubOrderNo": "0582715",
        "ItemNo": "20415541",
        "ItemType": "ART",
        "ItemName": "Fish",
        "TemplateName": "TP1234",
        "ObjectType": "MPP",
        "ObjectId": "PE1234",
        "SalesStartDate": "2018-08-01",
        "InfoText": "Some dummy text. This till be replaced later with some awesome text instead. Happy Fish!",
        "Attachment": null,
        "TemplateImage": null,
        "ApprovedBy": "Me",
        "ExpectedDeliveryDate": "2017-10-20",
        "Context": null,
        "TemplateDescription": null,
        "ColorStatus": 0,
        "spArticles": []
    }],
    "JsonOrders": null
}

I have validate this on json lint, so it's valid json.

I have the following code:

 public static DataTable jsonStringToTable(string jsonContent)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(jsonContent);
        return dt;
    }

When I run this, I get the error:

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

Anyone who can tell why I can't convert my json to datatable?

If you don't want to map your object you can try this way:

string json = File.ReadAllText("bryan.json");
dynamic result = JsonConvert.DeserializeObject(json);
Console.WriteLine(result.Orders[0].ObjectId);

This will create an object without giving a class, which will dinamically allocate the properties in the exactly same way as your js object

[Solved] JSON file to Datatable error, Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1. Here's my c# code:. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Unexpected JSON token when reading DataTable

Because your object is a Complex model from your JSON data. you can't use DataTable carry it.

So I would write models class to carry it.

public class Order
{
    public string SubOrderNo { get; set; }
    public string ItemNo { get; set; }
    public string ItemType { get; set; }
    public string ItemName { get; set; }
    public string TemplateName { get; set; }
    public string ObjectType { get; set; }
    public string ObjectId { get; set; }
    public string SalesStartDate { get; set; }
    public string InfoText { get; set; }
    public object Attachment { get; set; }
    public object TemplateImage { get; set; }
    public string ApprovedBy { get; set; }
    public string ExpectedDeliveryDate { get; set; }
    public object Context { get; set; }
    public object TemplateDescription { get; set; }
    public int ColorStatus { get; set; }
    public List<object> spArticles { get; set; }
}

public class RootObject
{
    public List<Order> Orders { get; set; }
    public object JsonOrders { get; set; }
}

Get model will be like.

var result= JsonConvert.DeserializeObject<RootObject>(jsonContent);

If you want let JSON to DataTable, you can read JSON key = Order

but there will be an error from "spArticles": [], if your want to DeserializeObject for DataTable

[{
    "SubOrderNo": "0582715",
    "ItemNo": "20415541",
    "ItemType": "ART",
    "ItemName": "Fish",
    "TemplateName": "TP1234",
    "ObjectType": "MPP",
    "ObjectId": "PE1234",
    "SalesStartDate": "2018-08-01",
    "InfoText": "Some dummy text. This till be replaced later with some awesome text instead. Happy Fish!",
    "Attachment": null,
    "TemplateImage": null,
    "ApprovedBy": "Me",
    "ExpectedDeliveryDate": "2017-10-20",
    "Context": null,
    "TemplateDescription": null,
    "ColorStatus": 0,
    "spArticles": []  //<--- There will excute a error, I report for Json.net will be fixed in future.  
}]

https://github.com/JamesNK/Newtonsoft.Json/issues/1763


So I would suggest you use models to carry your json data instead of DataTable

Getting an error trying to display data sent in a DataTable in , The error is: Unexpected JSON token when reading DataTable, Expected StartArray, got StartObject.Path ", line 1, position 1. I don't understand  Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1. Even if we give it come encouragement or prepare our json accordingly, then "object" types within the array can still prevent tabulation from occurring, where the deserializer doesn't know how to represent the objects in terms of

Use this one, I hope it will work.

//require .net Framework 4.5 above.

public static DataTable Tabulate(string jsonContent)
    {
        var jsonLinq = JObject.Parse(jsonContent);

        // 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());
    }

Code Editor, using Newtonsoft.Json;. 4. using System.Xml;. 5. ​. 6. //Unexpected JSON token when reading DataTable: StartObject. Path '[0].f1',. 7. ​. 8. public class Program. MVC api, DataTable in response - Unexpected JSON token while reading DataTable: StartObjectRSS 3 replies Last post Sep 11, 2014 12:54 PM by bmasnec

You need to deserialize your json string into object first and then use that object to convert into data table

public static DataTable jsonStringToTable(string jsonContent)
{
  dynamic jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent);
  DataTable dt = JsonConvert.DeserializeObject<DataTable>(Convert.ToString(jsonObject.Orders));
  return dt;
 }

How to extract the data from Json to Excel datatable - Build, Source: Deserialize json. Message: Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1  We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand

Newtonsoft.Json/DataTableConverter.cs at master · JamesNK , throw JsonSerializationException.Create(reader, "Unexpected JSON token when reading DataTable. Expected StartArray, got {0}.".FormatWith(CultureInfo. I have a json data look like var jsonString2 = @"[{""col1"": []}]"; I want to use DataTable dt = JsonConvert.DeserializeObject<DataTable>(jsonString2); It will throw an expection Unexpected JSON token when reading DataTable: EndArray.

How to Convert Json String into DataTable in ASP.Net with C# , Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1. Solution. To convert to DataTable You must​  JsonSerializer has a number of properties on it to customize how it serializes JSON. These can also be used with the methods on JsonConvert via the JsonSerializerSettings overloads. DateFormatHandling controls how dates are serialized. By default Json.NET writes dates in the ISO 8601 format, e.g. "2012-03-21T05:40Z".

https://social.msdn.microsoft.com/Forums/vstudio/e, Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1. dt = JsonConvert.

Comments
  • @MarcoSalemo: Im trying to convert my json to csv. Im following this solution: stackoverflow.com/questions/36274948/…
  • You can't do it, you have a complex object.
  • @Bryan if you want to convert to DataTable "spArticles": [] will be a problem
  • I tried to romve that, but I still get the same error.
  • @Bryan Here is remove "spArticles": [] json it can work good dotnetfiddle.net/MPpFqJ
  • What? I have already deserialized my object to a jsonstring? Why do it again? However, I get the following error when I run your code: Unexpected character encountered while parsing value: S. Path '', line 0, position 0.
  • You need to take jsonObject as dynamic not var. Updated my answer.
  • You have Orders as object key in your json string which can't be deserialized into data table directly, you need to access its inner json string.