I'm using SQL SERVER 2016 JSON result, but I don't know why it converts everything to array, e.g. if I execute the following query it returns an array instead of an object:
SELECT 1 AS One,2 AS Two,3 AS Three
FOR JSON PATH
The result is:
[{"One":1,"Two":2,"Three":3}]
But I would like it to return:
{"One":1,"Two":2,"Three":3}
Also I tested this query, but the result was the same, again an array:
SELECT TOP 1 1 AS One,2 AS Two,3 AS Three
FOR JSON PATH
An array is an ordered set of elements of a single built-in data type. An array can have an associated user-defined array type, or it can be the result of an SQL operation that returns an array value without an associated user-defined array type.
Arrays are a first class data type in the SQL standard, and generally allow for a simpler schema and more efficient queries. Arrays, in general, are a great data type.
The SQL Array Library uses the SQL Server binary data-type to store arrays. SQL Server supports to ways of storing binary data. If the structure size is under 8000 bytes it is stored in-page and the corresponding SQL data-type is varbinary(n) where n ≤ 8000.
Conclusion. As you can see, SQL Server does not include arrays. But we can use table variables, temporary tables or the STRING_SPLIT function. However, the STRING_SPLIT function is new and can be used only on SQL Server 2016 or later versions.
You just need the WITHOUT_ARRAY_WRAPPER
option:
SELECT 1 AS One,2 AS Two,3 AS Three
FOR JSON PATH ,WITHOUT_ARRAY_WRAPPER;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With