SQL Server 2016,我正在尝试使用一些常规数据并返回一个 JSON 对象以供另一个系统处理。另一个系统无法识别数组包装器,因此我尝试使用 WITHOUT_ARRAY_WRAPPER 来摆脱它。在子查询中使用时,会返回奇怪的结果...
SELECT @@SERVERNAME AS [Servername],
( SELECT [Name], [Recovery_Model_Desc]
FROM sys.databases
WHERE name in ('master', 'model', 'msdb')
FOR JSON PATH
) AS d
FOR JSON PATH, ROOT('ServerInformation')
这会产生预期的数据,使用数组包装器......
{"ServerInformation":[{"Servername":"MyServer","d":[{"Name":"master","Recovery_Model_Desc":"SIMPLE"},{"Name":"model","Recovery_Model_Desc" :"FULL"},{"Name":"msdb","Recovery_Model_Desc":"SIMPLE"}]}]}
但是, WITHOUT_ARRAY_WRAPPER 会产生...
SELECT @@SERVERNAME AS [Servername],
( SELECT [Name], [Recovery_Model_Desc]
FROM sys.databases
WHERE name in ('master', 'model', 'msdb')
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS d
FOR JSON PATH, ROOT('ServerInformation')
{"ServerInformation":[{"Servername":"MyServer","d":"{\"Name\":\"master\",\"Recovery_Model_Desc\":\"SIMPLE\"},{\"Name \":\"model\",\"Recovery_Model_Desc\":\"FULL\"},{\"Name\":\"msdb\",\"Recovery_Model_Desc\":\"SIMPLE\"}"}] }
而我希望它会产生
{"ServerInformation":{"Servername":"MyServer","d":{"Name":"master","Recovery_Model_Desc":"SIMPLE"},{"Name":"model","Recovery_Model_Desc":" FULL"},{"Name":"msdb","Recovery_Model_Desc":"SIMPLE"}}}
错误或预期结果?
编辑:调整预期结果
使用 json_query,
引入该功能时有一篇博客文章说:
帖子本身有点混乱和破碎,但我对此的解读是,它仅用于单行结果,因此嵌套的 JSON 将被视为字符串。