0

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?

Bryan
  • 3,421
  • 8
  • 37
  • 77

3 Answers3

1

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());
    }
Ethiraj
  • 117
  • 1
  • 12
0

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

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • @MarcoSalemo: Im trying to convert my json to csv. Im following this solution: https://stackoverflow.com/questions/36274948/json-string-to-csv-and-csv-to-json-conversion-in-c-sharp – Bryan Sep 18 '18 at 08:05
  • You can't do it, you have a complex object. – Marco Salerno Sep 18 '18 at 08:06
-1

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;
 }
Arun Kumar
  • 885
  • 5
  • 11
  • 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. – Bryan Sep 18 '18 at 07:57
  • You need to take jsonObject as dynamic not var. Updated my answer. – Arun Kumar Sep 18 '18 at 08:43
  • 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. – Arun Kumar Sep 18 '18 at 08:44