How to insert json data directly into table in sql.

Let us consider the below JSON as sample data,

ex: [{
"info": [{
"id": 1,
"name": "John",
"surname": "Smith",
"age": 25
},
{
"id": 2,
"name": "Abijal",
"surname": "Smith",
"age": 45
},
{
"id": 3,
"name": "Abhijit",
"surname": "singh",
"age": 41
}]
}]

To insert the above JSON we can use the below query,

Query:

declare @json varchar(max) =' [{ "info": [{ "id": 1, "name": "John", "surname": "Smith", "age": 25 }, { "id": 2, "name": "Abijal", "surname": "Smith", "age": 45 }, { "id": 3, "name": "Abhijit", "surname": "singh", "age": 41 }] }] ' Insert into EmployeeDetails SELECT Id,Age,FirstName,LastName from( SELECT * FROM OpenJson(@json)
WITH (inffo NVARCHAR(MAX) '$.info' AS JSON) OUTER APPLY OpenJson(inffo) WITH ( Id VARCHAR(30) '$.id',FirstName VARCHAR(30) '$.name',LastName VARCHAR(30) '$.surname',Age VARCHAR(30) '$.age') ) as empdetails

Popular posts from this blog

Implement virtualisation in kendo drop down list using jquery ajax in mvc, aspx page

How to Create trigger to Update the NoofOrgs Count under each category in Category Table based on Temp Table in Sql Server

How to Preview the image and download which is in base64string format in mvc view using C#