Skip to main content

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

In this article I will explain how to encrypt or decrypt connectionStrings in web.config file using asp.net. If we are using applications in our internal servers with security then it’s ok if we deploy our applications in shared host environment then we have chance to arise security problems to avoid these problems asp.net 2.0 provided built in protected configuration model functionality to encrypt or decrypt few sections of web.config file those are RSAProtectedConfigurationProvider :   This is default provider and uses the RSA public key encryption algorithm to encrypt and decrypt data. DataProtectionConfgurationProvider : This provider uses windows data protection application programming interface to encrypt and decrypt the data. The encrypting and decrypting of connection strings in web.config file will do by using aspnet_regiis.exe  command line tool and  code behind . First Method : First we will do encryption and decryption using  aspnet...

CRM Automate build Solution using Powershell commands

In CRM if there is any solution movement from other enviornment like DEV,PROD,STG we use to export the solution and import the solution to the respective environment by logging into MS Dynamics CRM. This will tends to extra effort to do manually for developers or release managers. So what if release user or test user can build and deploy the Solution deployment without CRM intervention(CRM loggin in) We need to do some automate build and deployment. In this article i write about how Import/Export solution automate to the various environment without intervention of logging into MS Dynamics CRM. XRM CI Framework,this is one tool use to automate the build in CRM       Below link is for download the XRM CI Framework https://xrmciframework.codeplex.com/releases/view/125516 Download and extract the ZIP file . First HelloWorld example “WhoAmIRequest” 1.Open "Powershell" and "Run as a Administrator" 2.Navigate to Extrac...

Auto number generation using C# and SQL

Auto number generation using C# and SQL ------------------------------------------------------ Req :  Create a Autonumber for Particular Application form(Eg:Order or Quote) Approach : ------------- 1.Create a Separate autonumber configuration table which looks like below AutoNumConfig table --------------------------   AutoNumberId  AutoNumber   FormNameorFormId   -------------------------------------------    1             ORD-0002     Order or 1066    2             QUO-0002     Quote or 1067 2.Create Transaction table for Application  Form(Eg:Order/Quote) which looks like below OrderTable -------------- OrderId   OrderName  OrderAutoNum -------------------------------------------   1       Spartan      ORD-0001   2       Xamarin      ORD-0002 QuoteT...