0
{"orders":[{"id":41,"order_number":41,"created_at":"2016-07-21T06:34:43Z","updated_at":"2016-07-21T06:34:43Z","completed_at":"2016-07-21T06:34:43Z","status":"processing","currency":"INR","total":"25000.00","subtotal":"25000.00","total_line_items_quantity":10,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":2,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/41","line_items":[{"id":8,"subtotal":"25000.00","subtotal_tax":"0.00","total":"25000.00","total_tax":"0.00","price":"2500.00","quantity":10,"tax_class":null,"name":"Yellow Saree","product_id":27,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":2,"created_at":"2016-07-21T05:58:17Z","email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","username":"harsh","role":"customer","last_order_id":"41","last_order_date":"2016-07-21T06:34:43Z","orders_count":3,"total_spent":"29600.00","avatar_url":"http:\/\/2.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ACE GROUP","address_1":"20 DHANLAXMI BUNGLOWS","address_2":"PAL","city":"SURAT","state":"GJ","postcode":"395009","country":"IN"}}},{"id":40,"order_number":40,"created_at":"2016-07-21T06:01:28Z","updated_at":"2016-07-21T06:01:28Z","completed_at":"2016-07-21T06:01:28Z","status":"processing","currency":"INR","total":"3600.00","subtotal":"3600.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":2,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/40","line_items":[{"id":6,"subtotal":"2100.00","subtotal_tax":"0.00","total":"2100.00","total_tax":"0.00","price":"2100.00","quantity":1,"tax_class":null,"name":"Orange Saree","product_id":31,"sku":"","meta":[]},{"id":7,"subtotal":"1500.00","subtotal_tax":"0.00","total":"1500.00","total_tax":"0.00","price":"1500.00","quantity":1,"tax_class":null,"name":"Test product","product_id":10,"sku":"testproduct","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":2,"created_at":"2016-07-21T05:58:17Z","email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","username":"harsh","role":"customer","last_order_id":"41","last_order_date":"2016-07-21T06:34:43Z","orders_count":3,"total_spent":"29600.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ACE GROUP","address_1":"20 DHANLAXMI BUNGLOWS","address_2":"PAL","city":"SURAT","state":"GJ","postcode":"395009","country":"IN"}}},{"id":39,"order_number":39,"created_at":"2016-07-21T05:58:17Z","updated_at":"2016-07-21T05:58:17Z","completed_at":"2016-07-21T05:58:17Z","status":"processing","currency":"INR","total":"1000.00","subtotal":"1000.00","total_line_items_quantity":1,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":2,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/39","line_items":[{"id":5,"subtotal":"1000.00","subtotal_tax":"0.00","total":"1000.00","total_tax":"0.00","price":"1000.00","quantity":1,"tax_class":null,"name":"Test Saree product","product_id":15,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":2,"created_at":"2016-07-21T05:58:17Z","email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","username":"harsh","role":"customer","last_order_id":"41","last_order_date":"2016-07-21T06:34:43Z","orders_count":3,"total_spent":"29600.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ACE GROUP","address_1":"20 DHANLAXMI BUNGLOWS","address_2":"PAL","city":"SURAT","state":"GJ","postcode":"395009","country":"IN"}}},{"id":38,"order_number":38,"created_at":"2016-07-21T05:24:29Z","updated_at":"2016-07-21T05:24:29Z","completed_at":"2016-07-21T05:24:29Z","status":"processing","currency":"INR","total":"3600.00","subtotal":"3600.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; rv:48.0) Gecko\/20100101 Firefox\/48.0","customer_id":0,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/38","line_items":[{"id":4,"subtotal":"3600.00","subtotal_tax":"0.00","total":"3600.00","total_tax":"0.00","price":"1800.00","quantity":2,"tax_class":null,"name":"Black Saree","product_id":23,"sku":"DM3103","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":0,"email":"harsh@resolutesolutions.in","first_name":"HARSH","last_name":"KANTHARIA","billing_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN","email":"harsh@resolutesolutions.in","phone":"9723638788"},"shipping_address":{"first_name":"HARSH","last_name":"KANTHARIA","company":"ITACHI GROUP","address_1":"20 dhanlaxmi bunglows","address_2":"nr nakshatra appartment,pal","city":"surat","state":"GJ","postcode":"395009","country":"IN"}}},{"id":37,"order_number":37,"created_at":"2016-07-20T08:17:21Z","updated_at":"2016-07-20T08:17:21Z","completed_at":"2016-07-20T08:17:21Z","status":"processing","currency":"INR","total":"2100.00","subtotal":"2100.00","total_line_items_quantity":1,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.3; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/51.0.2704.103 Safari\/537.36","customer_id":1,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/37","line_items":[{"id":3,"subtotal":"2100.00","subtotal_tax":"0.00","total":"2100.00","total_tax":"0.00","price":"2100.00","quantity":1,"tax_class":null,"name":"Orange Saree","product_id":31,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":1,"created_at":"2016-06-27T05:01:55Z","email":"info@resolutesolutions.in","first_name":"","last_name":"","username":"info@resolutesolutions.in","role":"administrator","last_order_id":"37","last_order_date":"2016-07-20T08:17:21Z","orders_count":3,"total_spent":"7300.00","avatar_url":"http:\/\/0.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}},{"id":35,"order_number":35,"created_at":"2016-06-27T08:09:00Z","updated_at":"2016-06-27T13:46:28Z","completed_at":"2016-06-27T13:46:28Z","status":"completed","currency":"INR","total":"4200.00","subtotal":"4200.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.3; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/51.0.2704.103 Safari\/537.36","customer_id":1,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/35","line_items":[{"id":2,"subtotal":"4200.00","subtotal_tax":"0.00","total":"4200.00","total_tax":"0.00","price":"2100.00","quantity":2,"tax_class":null,"name":"Orange Saree","product_id":31,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":1,"created_at":"2016-06-27T05:01:55Z","email":"info@resolutesolutions.in","first_name":"","last_name":"","username":"info@resolutesolutions.in","role":"administrator","last_order_id":"37","last_order_date":"2016-07-20T08:17:21Z","orders_count":3,"total_spent":"7300.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}},{"id":19,"order_number":19,"created_at":"2016-06-27T05:27:34Z","updated_at":"2016-06-27T05:27:34Z","completed_at":"2016-06-26T23:57:34Z","status":"processing","currency":"INR","total":"1000.00","subtotal":"1000.00","total_line_items_quantity":1,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on Delivery","paid":false},"billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN"},"note":"","customer_ip":"43.239.77.131","customer_user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko\/20100101 Firefox\/44.0","customer_id":1,"view_order_url":"http:\/\/woocommerce-5468-12693-128023.cloudwaysapps.com\/my-account\/view-order\/19","line_items":[{"id":1,"subtotal":"1000.00","subtotal_tax":"0.00","total":"1000.00","total_tax":"0.00","price":"1000.00","quantity":1,"tax_class":null,"name":"Test Saree product","product_id":15,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":1,"created_at":"2016-06-27T05:01:55Z","email":"info@resolutesolutions.in","first_name":"","last_name":"","username":"info@resolutesolutions.in","role":"administrator","last_order_id":"37","last_order_date":"2016-07-20T08:17:21Z","orders_count":3,"total_spent":"7300.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/?s=96","billing_address":{"first_name":"Rahul","last_name":"Patel","company":"Resolute Solutions","address_1":"surat","address_2":"","city":"Surat","state":"GJ","postcode":"395001","country":"IN","email":"info@resolutesolutions.in","phone":"9874563210"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}}]}

I am able to convert these string to datatable but i am not able to get all data which is inside the arrays in the json string, the data inside the arrays payment_details,billing_address,shipping_address,line_Items,shipping_lines,tax_lines,fee_lines,coupon_lines,customer and arrays inside customer. can anyone show me method how to do that. i need that data for c# windows application. i am new to this json stuff so if there is any change in question feel free to edit

  public static DataTable Tabulate(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());
    }

this is the code that i found online and i used this to convert the json to datatable

Sasuke Uchiha
  • 91
  • 1
  • 10
  • The problem is that your JSON has nested *objects* rather than nested *arrays of objects*, e.g.: `[{"id":41,"nested_object":{"name":"HARSH"}}]` instead of `[{"id":41,"nested_array":[{"name":"HARSH"}]}]`. A nested array can be converted to a nested `DataTable` but there's no canonical way to represent a nested object. What do you want to do there? – dbc Jul 22 '16 at 11:25
  • so if i want to convert the json to a datatable i need to have json in the form of nested array? can i convert this json to nested array ? @dbc – Sasuke Uchiha Jul 22 '16 at 11:45

2 Answers2

0

As your string is too long I use a shorter string (take from yours) which is easier to track and give you the clue.

First install the Json.NET (this is not the only option but it is one of the bests) using the Nuget package management. Then, you can easily parse the string. I guess the following snippets gives you idea.

using System;
using Newtonsoft.Json.Converters;

namespace Test
{
    class JsonParser
    {
        public static void Main(string[] args)
        {
            string jsonStr = @"{'orders':[
                            {'id':41,
                            'billing_address':{'first_name':'HARSH','last_name':'KANTHARIA','company':'ITACHI GROUP','address_1':'20 dhanlaxmi bunglows','address_2':'nr nakshatra appartment,pal','city':'surat','state':'GJ','postcode':'395009','country':'IN','email':'harsh@resolutesolutions.in','phone':'9723638788'},
                            }]}";
            dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr);
            Console.WriteLine(data.orders[0].billing_address.first_name);
            Console.Read();
        }        
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0

Json.NET has a built-in DataTableConverter. Thus you can convert a JArray to a DataTable by doing

var table = trgArray.ToObject<DataTable>();

However, your JSON has two problems that make this conversion fail:

  1. Some of your objects have empty array properties, for instance:

    "meta": []
    

    For some reason (possibly a bug?) these confuse the converter causing it to throw an exception. However, since they are empty, they can be removed and ignored:

    public static void RemoveEmptyArrayProperties(JContainer root)
    {
        var query = root.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Value is JArray && ((JArray)p.Value).Count == 0);
        foreach (var property in query.ToList())
        {
            property.Remove();
        }
    }
    
  2. It has objects nested inside objects. Simplifying greatly, you have the following:

    [
      {
        "id": 41,
        "customer": {
          "id": 2
        }
      }
    ]
    

    Unfortunately, there is no canonical way to convert this object hierarchy to a DataTable. It is possible to nest data tables inside each other, however this canonically maps to a nested JSON array rather than a nested object. In comments you wrote I need to insert that data in SQL Server. So, how do you want this data to be modeled in the database?

    One possibility would be to convert the nested objects to nested arrays of objects. Having done this, automatic conversion will be possible. However, it will now appear that each of your order objects can have multiple (e.g.) customers when this is in fact not true. If this is satisfactory then the following method can be used:

    public static void ConvertObjectPropertiesToArrays(JContainer root)
    {
        var query = root.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Parent is JObject && p.Value is JObject);
        foreach (var property in query.ToList())
        {
            var value = property.Value;
            var array = new JArray();
            property.Value = array;
            array.Add(value);
        }
    }
    

    It produces the following JSON:

    [
      {
        "id": 41,
        "customer": [
          {
            "id": 2
          }
        ]
      }
    ]
    

    Alternatively, you could flatten nested objects by bubbling their properties up to their parents. The following method does this:

    public static void FlattenObjectPropertiesToParents(JContainer root)
    {
        var query = root.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Parent is JObject && p.Value is JObject)
            .ToList();
        for (int i = query.Count - 1; i >= 0; i--)
        {
            var property = query[i];
            var value = (JObject)property.Value;
            var parent = (JObject)property.Parent;
            property.Remove();
            foreach (var item in value.Properties().ToList())
            {
                item.Remove();
                parent.Add(string.Format("{0}.{1}", property.Name, item.Name), item.Value);
            }
        }
    }
    

    It produces the following JSON which can be converted successfully to a DataTable:

    [
      {
        "id": 41,
        "customer.id": 2
      }
    ]
    

    Notice that the "id" property of the "customer" object has become "customer.id".

    As a third option, you could try deserializing nested objects as a Dictionary<string, object> -- though I have no idea whether the SQL Server adapter could handle such a data table. To do this, you will need to manually replicate the logic of DataTableConverter.ReadJson() along the lines of the answer to DateTime column type becomes String type after deserializing DataTable and modify GetColumnDataType(JsonReader reader) to return typeof(Dictionary<string, object>) for JsonToken.StartObject.

Thus your Tabulate() method becomes:

public static DataTable NestedTabulate(string json)
{
    // Find the first array using Linq
    var jsonLinq = (JContainer)JToken.Parse(json);
    var srcArray = jsonLinq.DescendantsAndSelf().OfType<JArray>().First();

    RemoveEmptyArrayProperties(srcArray);

    ConvertObjectPropertiesToArrays(srcArray);

    return srcArray.ToObject<DataTable>();
}

Or possibly:

public static DataTable FlattenTabulate(string json)
{
    // Find the first array using Linq
    var jsonLinq = (JContainer)JToken.Parse(json);
    var srcArray = jsonLinq.DescendantsAndSelf().OfType<JArray>().First();

    RemoveEmptyArrayProperties(srcArray);

    FlattenObjectPropertiesToParents(srcArray);

    return srcArray.ToObject<DataTable>();
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Thanks @dbc for your help this method of yours works and i am able to get all the data in the datatable thank you so much for your guidance and support – Sasuke Uchiha Jul 25 '16 at 10:22
  • i noticed that the data in the LineItems array is missing in the datatable that i get using the FlattenTabluate() function of your can you help me again Please. every other data is available only the one in the Lineitems section is not got @dbc – Sasuke Uchiha Jul 27 '16 at 05:57
  • @Harshkantharia - the `"line_items"` are a nested array of objects not a nested object. They are successfully deserialized by Json.NET as a nested `DataTable`. If you do `Debug.WriteLine(JsonConvert.SerializeObject(dt, Formatting.Indented))` on the returned `DataTable` you will see them. If they are not getting inserted into your database it must be because of how you are doing the insertion. Since the preferred format here is [one question per post](https://meta.stackexchange.com/questions/222735) I suggest you ask a 2nd question about why the insert fails. – dbc Jul 27 '16 at 12:42
  • thanks for your help @dbc i have managed to get the work done with the line items also i created another method to do that – Sasuke Uchiha Jul 29 '16 at 08:31