将 table 结果转换为 JSON 的形式

Convert table result to a form of JSON

我正在尝试从 table 结果集形成 json :

create table testmalc(
appid int identity(1,1),
propertyid1 int ,
propertyid1val varchar(10) ,
propertyid2 int,
propertyid2val varchar(10) ,
 ) 

insert into testmalc values(456,'t1',789,'t2')

insert into testmalc values(900,'t3',902,'t4')

需要低于预期的 JSON 结果:

{
    "data": {
        "record": [{
                "id": appid,
                "customFields": [{
                        "customfieldid": propertyid1 ,
                        "customfieldvalue": propertyid1val 
                    },
                    {
                        "customfieldid": propertyid2 ,
                        "customfieldvalue": propertyid2val 
                    }
                ]
            },
            {
                "id": appid,
                "customFields": [{
                        "customfieldid": propertyid1 ,
                        "customfieldvalue": propertyid1val 
                    },
                    {
                        "customfieldid": propertyid2 ,
                        "customfieldvalue": propertyid2val
                    }
                ]
            }
        ]
    }
}

我正在尝试使用 stuff,但没有得到想要的结果。现在尝试 UnPivot.

如果您无法升级到 SQL-Server 2016 以获得 JSON 支持,您应该尝试使用您知道的任何应用程序/编程语言来解决此问题。

只是为了好玩,我提供了一种可行的方法,但与其说是解决方案,不如说是一种技巧:

您的测试数据:

DECLARE @testmalc table (
appid int identity(1,1),
propertyid1 int ,
propertyid1val varchar(10) ,
propertyid2 int,
propertyid2val varchar(10)
);

insert into @testmalc values(456,'t1',789,'t2')
                           ,(900,'t3',902,'t4');

--创建一个XML,这是最相似的结构,读取为NVARCHAR字符串

DECLARE @intermediateXML NVARCHAR(MAX)=
(
SELECT t.appid AS id
      ,(
        SELECT t2.propertyid1 AS [prop1/@customfieldid]
              ,t2.propertyid1val AS [prop1/@customfieldvalue]
              ,t2.propertyid2 AS [prop2/@customfieldid]
              ,t2.propertyid2val AS [prop2/@customfieldvalue]
        FROM @testmalc t2 
        WHERE t2.appid=t.appid
        FOR XML PATH('customFields'),TYPE
       ) AS [*]
FROM @testmalc t
GROUP BY t.appid
FOR XML PATH('row')
);

--现在一堆替换

SET @intermediateXML=REPLACE(REPLACE(REPLACE(REPLACE(@intermediateXML,'=',':'),'/>','}'),'<prop1 ','{'),'<prop2 ','{');
SET @intermediateXML=REPLACE(REPLACE(REPLACE(REPLACE(@intermediateXML,'<customFields>','"customFields":['),'</customFields>',']'),'customfieldid','"customfieldid"'),'customfieldvalue',',"customfieldvalue"');
SET @intermediateXML=REPLACE(REPLACE(@intermediateXML,'<id>','"id":'),'</id>',',');
SET @intermediateXML=REPLACE(REPLACE(REPLACE(@intermediateXML,'<row>','{'),'</row>','}'),'}{','},{');

DECLARE @json NVARCHAR(MAX)=N'{"data":{"record":[' +  @intermediateXML + ']}}';

打印@json;

结果(格式化)

{
    "data": {
        "record": [
            {
                "id": 1,
                "customFields": [
                    {
                        "customfieldid": "456",
                        "customfieldvalue": "t1"
                    },
                    {
                        "customfieldid": "789",
                        "customfieldvalue": "t2"
                    }
                ]
            },
            {
                "id": 2,
                "customFields": [
                    {
                        "customfieldid": "900",
                        "customfieldvalue": "t3"
                    },
                    {
                        "customfieldid": "902",
                        "customfieldvalue": "t4"
                    }
                ]
            }
        ]
    }
}