How to create function return statement insert into table from json file ?

  • Thread starter Thread starter engahmedbarbary
  • Start date Start date
E

engahmedbarbary

Guest
  • 300
  • 11.2Problem
How to return statement insert into table values from json file ?
I work on newton soft library and I try to insert data to table master_table
but my pro
  1. insert into master_table(id,branch_id,name,address,phone) values (1,1,"bar","fleet street","555")
table ,keys,fields content flexible or dynamic .
my jsonfile D:\\1.json as below :
  1. {
  2. "master" : {
  3. "table": "master_table",
  4. "fields": {
  5. "name" : "bar",
  6. "address" : "fleet street",
  7. "phone" : "555"
  8. },
  9. "keys":{
  10. "id" : 1,
  11. "branch_id" : 1
  12. }
  13. }
  14. }
I already make get keys and fields but cannot make concatenate
insert statement as result .
How to concatenate insert statement that have keys + fields

as statement on first thread .

what i have tried


public static ExpandoObject ToExpando(string json)
{
if (string.IsNullOrEmpty(json))
return null;
return (ExpandoObject)ToExpandoObject(JToken.Parse(json));
}


private static object ToExpandoObject(JToken token)
{

switch (token.Type)
{
case JTokenType.Object:
var expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>)expando;
foreach (var prop in token.Children<JProperty>())
expandoDic.Add(prop.Name, ToExpandoObject(prop.Value));
return expando;
case JTokenType.Array:
return token.Select(ToExpandoObject).ToList();

default:
return ((JValue)token).Value;
}
}
static void Main(string[] args)
{
string JsonData = File.ReadAllText("D:\\1.json");
var ebj = SqlFactory.ToExpando (JsonData);
var name = (ebj as dynamic).master.table;
var fields = (ebj as dynamic).master.fields;
foreach (dynamic i in fields)
{
string key = i.Key;
object value = i.Value;
}
var keys = (ebj as dynamic).master.keys;
// i need after that create function return create insert statement
}

Continue reading...
 
Back
Top