Converting data types

As mentioned earlier, JSON does not have the same data types as SQL Server. Therefore, when JSON text is generated from relational data, a data type conversion is performed. The FOR JSON clause uses the following mapping to convert SQL Server data types to JSON types in the JSON output:

Conversion between SQL Server and JSON data types

SQL Server data type

JSON data type

Char, Varchar, Nchar, NVarchar, Text, Ntext, Date, DateTime, DateTime2, DateTimeOffset, Time, UniqueIdentifier, Smallmoney, Money, XML, HierarchyId, Sql_Variant

string

Tinyint, Smallint, Int, Bigint, Decimal, Float, Numeric

number

Bit

true or false

Binary, Varbinary, Image, Rowversion, Timestamp

encoded string (BASE 64) ...

Get SQL Server 2017 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.