Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get length of json array in SQL Server 2016

Tags:

You know about the new JSON_ support in SQL Server 2016 so let's say I have this data in a row

{   "BaseBoarding": 1,   "PriceLineStrategy": "PerPersonPerNight",   "Currency": "EUR",   "BasePriceLineList": [     {       "RoomTypeId": 1,       "PeriodId": 1,       "Price": 10.0     },     {       "RoomTypeId": 1,       "PeriodId": 2,       "Price": 100.0     },     {       "RoomTypeId": 1,       "PeriodId": 3,       "Price": 190.0     },     {       "RoomTypeId": 2,       "PeriodId": 1,       "Price": 280.0     },     {       "RoomTypeId": 2,       "PeriodId": 2,       "Price": 310.0     },     {       "RoomTypeId": 2,       "PeriodId": 3,       "Price": 340.0     }   ] } 

How do I get the number of items of "BasePriceLineList" in the most performant way, preferably using the built-in JSON support?

Need to write something like this:

SELECT JSON_ARRLEN(JsonDataCol, '$.BasePriceline') FROM MyTable WHERE Id = 1 

and get 6 as the result.

like image 407
Mihail Shishkov Avatar asked Mar 31 '17 13:03

Mihail Shishkov


People also ask

How do I get the length of a JSON array in SQL?

To obtain the size of a JSON-encoded array or object, use the json_size function, and specify the column containing the JSON string and the JSONPath expression to the array or object.

How do I find the length of an array in SQL Server?

You can use the LEN function () to find the length of a string value in SQL Server, for example, LEN (emp_name) will give you the length stored in the emp_name string. Remember that this is different from the actual length you specified when creating the table, for example, emp_name VARCHAR (60).

How do I query JSON data in SQL?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).


2 Answers

Using a table instead of a variable:

SELECT count(priceLineLists.RoomTypeId) FROM Mytable CROSS APPLY OPENJSON (JsonDataCol, N'$.BasePriceLineList')   WITH (     RoomTypeId int)       AS priceLineLists 
like image 175
rich kalasky Avatar answered Nov 25 '22 21:11

rich kalasky


You can convert it to a data set and then count the rows:

DECLARE @JSON NVARCHAR(4000) = N'{   "BaseBoarding": 1,   "PriceLineStrategy": "PerPersonPerNight",   "Currency": "EUR",   "BasePriceLineList": [     {       "RoomTypeId": 1,       "PeriodId": 1,       "Price": 10.0     },     {       "RoomTypeId": 1,       "PeriodId": 2,       "Price": 100.0     },     {       "RoomTypeId": 1,       "PeriodId": 3,       "Price": 190.0     },     {       "RoomTypeId": 2,       "PeriodId": 1,       "Price": 280.0     },     {       "RoomTypeId": 2,       "PeriodId": 2,       "Price": 310.0     },     {       "RoomTypeId": 2,       "PeriodId": 3,       "Price": 340.0     }   ] }'  select COUNT(*)  FROM OPENJSON(@json, N'$.BasePriceLineList') WITH (RoomTypeID varchar(100) '$.RoomTypeId') 
like image 44
dfundako Avatar answered Nov 25 '22 20:11

dfundako