Can someone give example how to pass JSON type as parameter to SQL Server 2016 stored procedure using ADO.Net in C# ASP.Net Core Web Api project ? I want to see example of SQL Server 2016 stored procedure and pass of JSON type in C# ASP.Net Core Web Api.
-
2Very good question. I want too– Fabio Silva LimaDec 27, 2016 at 0:31
-
1AFIAK, Sql server 2016 does not have a JSON data type, so you just pass your json to the stored procedure as a varchar. Inside the stored procedure you can use the built in functions that knows how to handle JSON data. You can see a code example in this article.– Zohar PeledJan 2, 2017 at 9:19
-
There are two sentences in your question and both of them say exactly the same thing. And then the title of your question says exactly the same thing again. Please clean up your question.– TomalakJan 2, 2017 at 12:31
3 Answers
There is no json
data type in SQL Server you can simply send your json
as nvarchar(max)
to a stored procedure.
If you want to map your json to table you can use use OPENJSON
to convert data to rows and columns.
CREATE PROCEDURE SaveJSON
@pID int,
@pJson nvarchar(max)
AS
BEGIN
INSERT INTO [YourTable] ([ID], [JSONData])
VALUES (@pID, @pJson)
END
If you want to map json objects with table you can do this
//json would be something like this
[
{ "id" : 2,"name": "John"},
{ "id" : 5,"name": "John"}
]
INSERT INTO YourTable (id,Name)
SELECT id, name
FROM OPENJSON(@pJson)
WITH (id int,
name nvarchar(max))
Here is a very good and detailed article which will give you detailed idea to deal with json data
-
As per expectation,thank you so much for this awesome solution Mar 2, 2020 at 10:57
-
for add `JSON Data type' native vote this : feedback.azure.com/forums/908035-sql-server/suggestions/… Nov 27, 2020 at 14:52
-
Actually, the data type needs to be
nvarchar(max)
(not justvarchar
as you mentioned) - corrected– marc_sJul 11, 2021 at 12:13 -
I have found that there are 2 requirements to use this nice little inline table function: 1. SQL Server 2016 and later and database compatibility >= 130
select compatibility_level from sys.databases
– jvndevApr 21, 2022 at 17:04
SQL Server 2016 do have native JSON support - a new JSON datatype (which is based on nvarchar
) is there, as well as a FOR JSON command to convert output from a query into JSON format
Microsoft did not include a separate JSON datatype - instead, there are a number of JSON functions (to package up database rows into JSON, or to parse JSON into relational data) which operate on columns of type NVARCHAR(n)
If you have JSON text, you can extract data from JSON or verify that JSON is properly formatted using built-in functions JSON_VALUE
, JSON_QUERY
, and ISJSON
. For more advanced querying and analysis, the OPENJSON
function can transform an array of JSON objects into a set of rows. Any SQL query can be executed on the returned result set. Finally, there is the FOR JSON
clause that enables you to format query results as JSON text.
So, I recommend you use NVARCHAR(MAX)
as your stored procedure parameter.
For a simple example that shows the C# and the SQL, please see: https://chris.koester.io/index.php/2018/03/21/load-json-into-sql-server-using-a-stored-procedure-and-csharp/
You probably have some higher level data layer approach in your C#, like with EF or something, but this will let you get a simple test setup using a good ol' ADO.Net SQLCommand.