[英]sql output json structure
我需要获得以下 output 关于 sql 查询:
{
"records": [
{
"attributes": {
"type": "customer"
},
"name": "test 1",
"email": "test1@test.com"
},
{
"attributes": {
"type": "customer"
},
"name": "test 2",
"email": "test2@test2.com"
}
]
}
我的尝试:
DECLARE @ttCustomer table (cname nvarchar(50),
email nvarchar(250));
INSERT @ttCustomer
VALUES ('test 1', 'test1@test.com');
INSERT @ttCustomer
VALUES ('test 2', 'test2@test.com');
SELECT 'customer' AS [records.attributes.type],
cname AS [records.name],
email AS [records.email]
FROM @ttCustomer
FOR JSON PATH;
几乎完美的 output 结果
[
{
"records": {
"attributes": {
"type": "customer"
},
"name": "test 1",
"email": "test1@test.com"
}
},
{
"records": {
"attributes": {
"type": "customer"
},
"name": "test 2",
"email": "test2@test.com"
}
}
]
只需定义您的ROOT
:
DECLARE @ttCustomer table (cname nvarchar(50),
email nvarchar(250));
INSERT @ttCustomer
VALUES ('test 1', 'test1@test.com');
INSERT @ttCustomer
VALUES ('test 2', 'test2@test.com');
SELECT 'customer' AS [attributes.type],
cname AS [name],
email AS [email]
FROM @ttCustomer
FOR JSON PATH, ROOT('records');
输出:
{
"records": [
{
"attributes": {
"type": "customer"
},
"name": "test 1",
"email": "test1@test.com"
},
{
"attributes": {
"type": "customer"
},
"name": "test 2",
"email": "test2@test.com"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.