Saman Gholami Saman Gholami - 1 month ago 5
JSON Question

Why SQL returns an Array instead of an Object?

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

Answer

You just need the WITHOUT_ARRAY_WRAPPER option:

SELECT 1 AS One,2 AS Two,3 AS Three
FOR JSON PATH ,WITHOUT_ARRAY_WRAPPER; 
Comments