您也许可以利用我过去使用过的一些基本的 sql 到 json 逻辑......但它可能特定于我的数据集。我试着把它概括一下。
SET NOCOUNT ON;
--sample table
CREATE TABLE #Temp(
Id INT Identity(1,1),
Column1 INT,
Column2 VARCHAR(10),
Column3 VARCHAR(10)
)
;
INSERT INTO #Temp(Column1, Column2, Column3) VALUES (10,'Test', 'Test2'), (20, 'Test3', 'Test4'), (30, 'Test5', 'Test6');
WITH
cte AS(
SELECT Id AS RowId,
CAST(Id AS VARCHAR(100)) AS Id,
CAST(Column1 AS VARCHAR(100)) AS Column1,
CAST(Column2 AS VARCHAR(100)) AS Column2,
CAST(Column3 AS VARCHAR(100)) AS Column3
FROM #Temp
),
cte2 AS (
SELECT RowId,
'"' + PropertyName + '"' + ':' + CASE WHEN ISNUMERIC(Value) = 1 THEN Value ELSE '"' + Value + '"' END AS Value,
ROW_NUMBER() OVER(PARTITION BY RowId ORDER BY CASE WHEN PropertyName = 'Id' THEN '' ELSE PropertyName END) AS RowNum,
ROW_NUMBER() OVER(ORDER BY RowId) AS RowNum2
FROM cte
UNPIVOT(
Value
FOR PropertyName IN (
Id,
Column1,
Column2,
Column3
)
) upvt
)
SELECT CASE WHEN cte2.RowNum2 = y.MinRowNum THEN '[' ELSE '' END,
CASE WHEN cte2.RowNum = x.MinRowNum THEN '{' ELSE '' END,
cte2.value,
CASE WHEN cte2.RowNum <> x.MaxRowNum THEN ',' ELSE '' END,
CASE
WHEN cte2.RowNum = x.MaxRowNum THEN '}' +
CASE WHEN cte2.RowNum2 = y.MaxRowNum THEN '' ELSE ',' END
ELSE ''
END,
CASE WHEN cte2.RowNum2 = y.MaxRowNum THEN ']' ELSE '' END
FROM cte2
INNER JOIN (
SELECT RowId,
MIN(RowNum) AS MinRowNum,
MAX(RowNum) AS MaxRowNum
FROM cte2
GROUP BY RowId
) x
ON cte2.RowId = x.RowId
CROSS JOIN (
SELECT MIN(RowNum2) AS MinRowNum,
MAX(RowNum2) AS MaxRowNum
FROM cte2
) y
;
/* --输出如下:
[ { "Id":1 ,
"Column1":10 ,
"Column2":"Test" ,
"Column3":"Test2" },
{ "Id":2 ,
"Column1":20 ,
"Column2":"Test3 " ,
"Column3":"Test4" },
{ "Id":3 ,
"Column1":30 ,
"Column2":"Test5" ,
"Column3":"Test6" } ] */