帶有列別名的 FOR JSON PATH

FOR JSON PATH 使你可以使用列別名控制輸出 JSON 的格式:

SELECT top 3 object_id as id, name as [data.name], type as [data.type]
FROM sys.objects
FOR JSON PATH

列別名將用作鍵名。以點分隔的列別名(data.name 和 data.type)將作為巢狀物件生成。如果兩列在點表示法中具有相同的字首,則它們將在單個物件(本示例中的資料)中組合在一起:

[
  {"id":3,"data":{"name":"sysrscols","type":"S "}},
  {"id":5,"data":{"name":"sysrowsets","type":"S "}},
  {"id":6,"data":{"name":"sysclones","type":"S "}}
]