我在 SQL 服务器中有这个结果集:
ID CUSTOMER PRODUCT DATE COUNT
A1 Walmart Widget 1/1/2020 5
B2 Amazon Thingy 1/2/2020 10
C3 Target Gadget 2/1/2020 7
我想将它输出为 json,SQL server 2016+ 有很多功能。但我想要一个由 id 索引的传统字符串索引列表(“字典”),如下所示:
目标
{
"A1": {"Customer":"Walmart", "Product":"Widget", "Date":"1/1/2020", "Count":5 },
"B2": {"Customer":"Amazon", "Product":"Thingy", "Date":"1/2/2020", "Count":10},
"C3": {"Customer":"Target", "Product":"Gadget", "Date":"2/1/2020", "Count":7 }
}
但是,典型的select * from table for json path
输出为未索引的对象数组:
当前状态
[
{"Id":"A1", "Customer":"Walmart", "Product":"Widget", "Date":"1/1/2020", "Count":5 },
{"Id":"B2", "Customer":"Amazon", "Product":"Thingy", "Date":"1/2/2020", "Count":10},
{"Id":"C3", "Customer":"Target", "Product":"Gadget", "Date":"2/1/2020", "Count":7 }
]
其他for json
修饰符,如root
表面上相关,但据我所知,只是美化了在外部根节点中捕获整个对象的字符串连接。
如何使用本机(高性能)SQL 服务器json
函数完成上述表示法?