How to force SQL Server to return empty JSON array How to force SQL Server to return empty JSON array json json

How to force SQL Server to return empty JSON array


You can always check this with ISNULL, e.g.:

select ISNULL( (SELECT * FROM sys.tables where 1=2 FOR JSON PATH), '[]')

If you need this in app layer, maybe it would be better to check is there some results set in data access code, and if not just return [] or {}.


This works, and can be composed within another for json query:

select json_query('[]') arr     for json path, without_array_wrapper


A little manual, but if you need a quick hack, here you go:

DECLARE @JSON NVARCHAR(MAX) = (SELECT NULL AS testFOR JSON PATH,ROOT('arr'))SELECT REPLACE(@json, '{}', '')