HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Alternative to "FOR JSON" in SQL Server 2012?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
alternative2012serversqlforjson

Problem

In SQL Server 2016 I can output query results as JSON as described in the documentation Format Query Results as JSON with FOR JSON (SQL Server)

SELECT * 
FROM x 
FOR JSON PATH


But I'm finding that this is not supported in SQL Server 2012.

Is there a built-in way to convert tabular data to JSON data in SQL Server 2012?

To make this cross compatible I would ideally like to be able to use for json path in SQL Server 2012.

Solution

No. JSON support was new in SQL Server 2016.

If you want to convert data to JSON, you have a few options:

  • Fetch the rows as you would any other query result, and convert it in the application tier (like C#, Java, etc)



  • Build T-SQL logic in a stored procedure or function to convert it to JSON yourself (but keep in mind that you won't be able to use "FOR JSON PATH" the exact same way 2016 does)



  • Build CLR logic to do it (again, won't be compatible with "FOR JSON PATH")

Context

StackExchange Database Administrators Q#198838, answer score: 6

Revisions (0)

No revisions yet.