I have an Azure function written in C# which receives JSON by HTTP POST request, in which I am sure about nodes: ssid, dim, type, list_of_business_keys. Each item in the list, has one or more columns, which name and type I do not know, before I receive the JSON. Let's say that for the first example they are column_1 and column_2, both of Int64 type:
{ "ssid" : 1,
"dim" : 2,
"type" : 3,
"list_of_business_keys":
[
{"business_key" : {"column_1" : 100, "column_2" : 1000}},
{"business_key" : {"column_1" : 200, "column_2" : 1000}},
{"business_key" : {"column_1" : 300, "column_2" : 1000}},
{"business_key" : {"column_1" : 400, "column_2" : 1000}},
{"business_key" : {"column_1" : 500, "column_2" : 1000}}
]
}
What I would like to achieve is to convert this JSON to a DataTable, that I would later use as a table type parameter to invoke a Stored Procedure from Azure SQL Database. So I would like this DataTable to look like this:
I have written following code to achieve that:
#r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"
#r "System.Net"
#r "System.Data"
using System;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
string resultAsString = await req.Content.ReadAsStringAsync();
KeyList keyList = JsonConvert.DeserializeObject<KeyList>(resultAsString);
List<ListOfBusinessKey> list = keyList.list_of_business_keys;
DataTable tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ssid", typeof(Int64)));
tbl.Columns.Add(new DataColumn("dim", typeof(Int64)));
tbl.Columns.Add(new DataColumn("type", typeof(Int64)));
tbl.Columns.Add(new DataColumn("column_1", typeof(Int64)));
tbl.Columns.Add(new DataColumn("column_2", typeof(Int64)));
foreach (var key in list) {
tbl.Rows.Add(keyList.ssid, keyList.dim, keyList.type, key.business_key.column_1, key.business_key.column_2);
}
foreach (var row in tbl.Rows){
log.Info(row.ToString());
}
foreach (DataRow dataRow in tbl.Rows)
{
foreach (var item in dataRow.ItemArray)
{
log.Info(item.ToString());
}
}
return req.CreateResponse(keyList);
}
public class BusinessKey
{
public int column_1 { get; set; }
public int column_2 { get; set; }
}
public class ListOfBusinessKey
{
public BusinessKey business_key { get; set; }
}
public class KeyList
{
public int ssid { get; set; }
public int dim { get; set; }
public int type { get; set; }
public List<ListOfBusinessKey> list_of_business_keys { get; set; }
}
And this seems to work.
My problem is that to deserialize received JSON I have to create classes based on JSON's structure. The same is with creating the data table. I know the col names and their types so I create the DataTable implicitly. But what happens when I do not know the structure before?
So let's now I assume that I receive following JSON:
{ "ssid" : 1,
"dim" : 2,
"type" : 3,
"list_of_business_keys":
[
{"business_key" : {"xxx" : "abc", "yyy" : 1000, "zzz" : 123}},
{"business_key" : {"xxx" : "cde", "yyy" : 1000, "zzz" : 456}},
{"business_key" : {"xxx" : "efg", "yyy" : 1000, "zzz" : 789}},
{"business_key" : {"xxx" : "hij", "yyy" : 1000, "zzz" : 12 }},
{"business_key" : {"xxx" : "klm", "yyy" : 1000, "zzz" : 345}}
]
}
and I would like to receive following DT:
Is this possible to change the code, to "dynamically" convert JSON to desired format of DataTable? If yes, what should I do to achieve that?
Thanks in advance!