Search code examples
sqlsql-serverjsonsql-server-2014

Get result of sql query in JSON format via MSSQL


I need to get result of sql query in JSON format via MSSQL 2014. I have wrote this code:

SELECT bc.Id, bc.CityName, bc.ProvinceId FROM BusCity AS bc WHERE bc.Country=2 FOR JSON AUTO

But I am getting an error:

Incorrect syntax near 'JSON'.

Is this possible? How to do this?


Solution

  • You running 2016 or higher?

    FOR JSON was introduced then.

    Type

    SELECT @@Version
    

    To find out what version you're running.

    https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server