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

How to get the user details, user current level, next level, Rank in sql server

How to update the user level when ever user report any tweet in sql

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