0

My service returns JArray and I wanted to know the best way to convert JArray to object[,] in c#.

Below are some example of JSON JArray responses.

Sample 1:

[{"id":"281","Name":"Sam","FamilyName":"Smith"},{"id":"348","Name":"Dough","FamilyName":"White"}]

Expected result of sample 1:

enter image description here

Sample 2:

[{"Number":"281","CustomerId":"Sam01","UserName":"Smith","Date":"01/01/2020"},{"Number":"348","CustomerId":"Dough01","UserName":"White","Date":"01/01/2020"}]

Expected result of sample 2:

enter image description here

I do not know how many columns and type I would get from my service. That is why I want to convert to object[,].

Currently, I am converting JArray into DataTable and then looping each row, columns to create object[,], I think this is slow.

    DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
var multiDimensionalArry = new Object[dt.Rows.Count + 1, dt.Columns.Count];

                    for (int i = 0; i < dt.Columns.Count; i++)
                        multiDimensionalArry[0, i] = dt.Columns[i].ColumnName;

                    for (int j = 1; j <= dt.Rows.Count; j++)
                    {
                        var rowItem = dt.Rows[j - 1].ItemArray;
                        for (int k = 0; k <= rowItem.Length - 1; k++)
                        {
                            if (string.IsNullOrEmpty(rowItem[k].ToString()))
                                multiDimensionalArry[j, k] = "";
                            else
                                multiDimensionalArry[j, k] = rowItem[k];
                        }
                    }
user781700
  • 844
  • 3
  • 15
  • 27
  • 1
    Why not just `JsonConvert.DeserializeObject[]>(json)`? – Guru Stron Jul 01 '20 at 22:12
  • 1
    I don't entirely understand what you want. If you convert an array of JSON objects to a 2d array of values, you will strip off the property names of the objects, leaving only the values, right? See https://dotnetfiddle.net/P6ruPm for a demonstration. But your images somehow show the property names. How do you intend to represent the property names? What are the exact contents required for the `object [,]` array? Can you show how you currently do this with `DataTable`? – dbc Jul 01 '20 at 22:47
  • *Currently, I am converting JArray into DataTable and then looping each row, columns to create object[,], I think this is slow.* - have you profiled it to determine whether you have a bottleneck? In your code sample you are not converting a `JArray` to a `DataTable`, you are deserializing directly from a `string`. Do you ever actually parse into a `JArray`? – dbc Jul 01 '20 at 22:48
  • @GuruStron, How can I convert JsonConvert.DeserializeObject[]>(json) into object[,]? – user781700 Jul 02 '20 at 14:16
  • @user781700 not sure why do you need `object[,]` =) – Guru Stron Jul 02 '20 at 14:19
  • @dbc I updated the question with 2d array code. When I have ~ 1000 rows and 10 columns its super fast. But when I have ~1 Million rows and 100 columns it takes visibly long time. – user781700 Jul 02 '20 at 14:22
  • @GuruStron I am displaying the data into excel and it requires the data in specific format. – user781700 Jul 02 '20 at 14:23
  • @user781700 - *when I have ~1 Million rows and 100 columns it takes visibly long time.* - how big is that file? Have you profiled it? See https://ericlippert.com/2012/12/17/performance-rant/: ***Use a profiler or other analysis tool to determine empirically where the bottleneck is before you start investigating alternatives.*** It might just be that that is how long it takes to read a JSON file of that size. How long does it take to read through the file using a `JsonTextReader` by doing `while (reader.Read()) ;` ? – dbc Jul 02 '20 at 14:30
  • Lacking a [mcve] I made a synthetic example with 100000 rows and 100 columns. Your code took 12.2 seconds to parse. A simple `Read()` looks was 77% faster at 3.4 seconds -- that's the max performance you could get. By substituting a `List>` for the data table I sped it up by roughly 45% to 6.74 seconds. Is that what you're looking for? Demo fiddle here https://dotnetfiddle.net/sjfb18 (which uses a much smaller number of rows). – dbc Jul 03 '20 at 19:56

0 Answers0