【发布时间】:2022-01-22 04:03:12
【问题描述】:
我正在尝试以这种格式从我的 SSMS 中的 SQL 数据生成 JSON:
{
"id": "1234",
"first_name": "Hasan",
"last_name": "Mahmud",
"custom_fields": [
{
"custom_field_name": "CPRCertified",
"custom_field_value": "Y"
},
{
"custom_field_name": "IsAttorney",
"custom_field_value": "N"
}
]
}
我正在尝试这个:
SELECT e.Empl_ID AS id,
e.FirstName AS first_name,
e.LastName AS last_name,
'CPRCertified' AS [custom_fields.custom_field_name],
e.CPRCertified AS [custom_fields.custom_field_value],
'IsAttorney' AS [custom_fields.custom_field_name],
e.IsAttorney AS [custom_fields.custom_field_value]
FROM #e e
WHERE e.Empl_ID = '1234'
FOR JSON PATH;
但我收到此错误:
无法在 JSON 中生成属性“custom_fields.custom_field_name”
由于与另一个列名或别名冲突而导致的输出。使用
SELECT 列表中每一列的不同名称和别名。
我已经尝试过这个主题SQL to JSON - Grouping Results into JSON Array,但由于我多次使用相同的“custom_field_name”而无法正常工作。
【问题讨论】:
标签: sql json sql-server