将表格结果转换为JSON形式

时间:2019-03-01 08:02:25

标签: sql-server-2008

我正在尝试从表结果集中形成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')

enter image description here

需要低于所需的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

1 个答案:

答案 0 :(得分:1)

如果您无法升级到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 + ']}}';

PRINT @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"
                    }
                ]
            }
        ]
    }
}