C# Convert array of objects to flat (CSV/JSON array) table

Date: 2019-06-18
    public class DataHelper
    {
        public static List<object[]> GetDataTable<T>(IEnumerable<T> list)
        {
            Type type = typeof(T);
            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var rows = new List<object[]>();
            var headerRow = new object[properties.Length];
            for (int i = 0; i < properties.Length; i++)
            {
                var name = properties[i].Name;
                headerRow[i] = char.ToLowerInvariant(name[0]) + name.Substring(1);
            }
            rows.Add(headerRow);
            foreach(var item in list) { 
                var row = new object[properties.Length];
                for (int i = 0; i < properties.Length; i++)
                {
                    row[i] = properties[i].GetValue(item);
                }
                rows.Add(row);
            }  
            return rows;
        }
    }

Parse the JSON datatable in typescript

const fromDataTable = (data: any[]): any[] => {
    const firstRow = data.shift();
    return data.map((row) => firstRow.reduce((obj, key, i) => ((obj[key] = row[i]) || 1) && obj, {}));
};
22090cookie-checkC# Convert array of objects to flat (CSV/JSON array) table