1

How do I insert a JSON integer array to a table column in SQL server. Suppose I have the following json variable:

declare @jsonArray as varchar(max);
set @jsonArray = '[1,2,3,4]';

How do I insert the integer values into a table's integer type column using some of the SQL server JSON statements/functions?

1 Answer 1

2

You can use openjson to split the JSON data-

declare @jsonArray as varchar(max);
set @jsonArray = '[1,2,3,4]';
SELECT Value FROM OPENJSON(@jsonArray)

Value
-----------
1
2
3
4

(4 rows affected)

Please use like below

declare @jsonArray as varchar(max);
set @jsonArray = '[1,2,3,4]';
INSERT INTO yourTableName(ColumnName)
SELECT Value FROM OPENJSON(@jsonArray)

Read more from - https://msbiskills.com/2018/01/22/new-t-sql-features-in-sql-server-2016-xii-openjson-function-sql-server/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I had seen some very complex json object examples, but didn't find anything simple as an integer array. Also didn't know about the "Value" keyword. That's something new for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.