I would like to output the results of a SELECT statement as a JSON object.
I would like this to be a Function and not a stored procedure!
For example, the below table Users
id name active
1 Bob Jones 1
2 John Smith 0
Would be returned like this:
[{"id":1,"name":"Bob Jones","active":1},{"id":2,"name":"John Smith","active":0}]
Thanks in advance.
Starting from SQL Server 2016 you can use
for json:With older versions of SQL Server you can use
for xml path, e.g.:Output: