使用SQLServer将表转换为嵌套的json

xpcnnkqh  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(232)
I have following table 

        ID   value1    value2    value3      value4
        ---------------------------------------------        
         A     A1        A1.1        A1.2     A1.3
         A     A2        A2.1        A2.2     A2.3
         A     A3        A3.1        A3.2     A1.3
         B     B1        B1.1        B1.2     B1.3
         B     B2        B2.1        B2.2     B2.3

创建表#sample(id nvarchar(10)、value1 nvarchar(10)、value2 nvarchar(10)、value3 nvarchar(10)、value4 nvarchar(10))
我想把表数据转换成嵌套的json格式。有谁能告诉我如何写一个查询和获取结果如下,我已经尝试了stuff,pivot,coalesce。所有结果返回错误的json格式。
预期输出为:

[{
        "A": [{
                "value1": "A1",
                "value2": "A1.1",
                "value3": "A1.2",
                "value4": "A1.3"
            },
            {
                "value1": "A2",
                "value2": "A2.1",
                "value3": "A2.2",
                "value4": "A2.3"
            },
            {
                "value1": "A3",
                "value2": "A3.1",
                "value3": "A3.2",
                "value4": "A3.3"
            }
        ]
    },
    {
        "B": [{
                "value1": "B1",
                "value2": "B1.1",
                "value3": "B1.2",
                "value4": "B1.3"
            },
            {
                "value1": "B2",
                "value2": "B2.1",
                "value3": "B2.2",
                "value4": "B2.3"
            }
        ]
    }  
    ]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题