Parse JSON file and insert data into SQL server

Parse JSON file and insert data into SQL server
When there any requirement, insert json data into sql server
 the following blog will explain step by step to insert the data using sql server


Sample JSON :
     First I'd restructure the JSON so it made more sense. You said a "person" can have multiple "people", so structure it that way. A "person" has three attributes (i.e. i_date, i_location, and i_summary) and a collection of people.

{
"person":{
    "i_date":"2014-03-20",
    "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 .NET classes that represent the structure


  public class Person2
  {
    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<Person2> people { get; set; }
}

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

The above class can be easily generated by using the following url
                              http://json2csharp.com/
When you give the sample JSON it will convert .net class automatically.
   var root = JsonConvert.DeserializeObject<RootObject>( json );
 
 You need to use Newtonsoft Json download from Nuget Package from Visual studio and make use of above feature.
  With Above Object "root" u can perform the Database logic.

Comments

Popular posts from this blog

Asp.net- Encrypt and Decrypt connection strings in web.config file

CRM Automate build Solution using Powershell commands

Access the fields on Quick View Form – Dynamics CRM 2015