-2

Can someone please provide example How to Insert JSON file in SQL tables using C#.

user2741746
  • 1
  • 1
  • 2
  • 1
    Please read [ask] and explain very explicitly what you mean by "insert JSON". JSON is just a string. Do you want to save it as a string? What is your question then? – CodeCaster Feb 23 '17 at 07:27
  • I want to store JSON file data into SQL table using C#. by reading File and converting data in tabular format. – user2741746 Feb 23 '17 at 07:39

1 Answers1

1

Say you have the following JSON file:

{
  "person":{
    "i_date":"2017-02-23",
    "i_location":"test",
    "i_summary":"test test",
    "people":[
      {
        "first_name":"first name test1",
        "last_name":"last name test1"
      },
      {
        "first_name":"first name test2",
        "last_name":"last name test2"
      },
      {
        "first_name": "first name test3",
        "last_name":"last name test3"
      }
    ]
  }
}

Now you can declare some classes that represent the structure:

public class PersonalPerson
{
    public string first_name { get; set; }
    public string last_name { get; set; }
}

public class Person
{
    public string i_date { get; set; }
    public string i_location { get; set; }
    public string i_summary { get; set; }
    public List<PersonalPerson> people { get; set; }
}

public class RootObject
{
    public Person person { get; set; }
}

Finally, use JsonConvert.DeserializeObject to get a set of object instances.

var root = JsonConvert.DeserializeObject<RootObject>( json );

You can now iterate over the "people" attached to the "person" and do stuff with it. At this point you can use either ADO.NET or Entity Framework to transfer the values from the objects into either SQL Parameters (ADO.NET) or EF classes to persist it into the database.

I hope that provides you the information you've needed

DeJaVo
  • 3,091
  • 2
  • 17
  • 32
  • Anotehr option is to follow the description here:http://stackoverflow.com/questions/7641004/how-to-convert-json-into-datatable – DeJaVo Feb 23 '17 at 07:28