如何在没有创建嵌套对象的情况下在FOR JSON PATH的列名中使用点/句点?

时间:2017-05-09 13:06:07

标签: sql-server json

给出以下脚本:

DECLARE @table1 TABLE (t1num int NOT NULL);
DECLARE @table2 TABLE (t2num int NOT NULL);
DECLARE @table3 TABLE (t3num int NOT NULL);

INSERT INTO @table1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table2 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table3 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);


SELECT
    t2num,
    (
        SELECT t1num AS [t1.num]
        FROM @table1
        FOR JSON PATH
    ) AS t1s
FROM @table2 [t2]

t1s列的输出如下所示:

[{"t1":{"num":1}},{"t1":{"num":2}},{"t1":{"num":3}},{"t1":{"num":4}},{"t1":{"num":5}},{"t1":{"num":6}},{"t1":{"num":7}},{"t1":{"num":8}},{"t1":{"num":9}},{"t1":{"num":10}}]

但是,我需要它看起来像这样:

[{"t1_num":1},{"t1_num":2},{"t1_num":3},{"t1_num":4},{"t1_num":5},{"t1_num":6},{"t1_num":7},{"t1_num":8},{"t1_num":9},{"t1_num":10}]

...除了句号而不是下划线。

如果我尝试双点,SQL Server会返回此错误:

Msg 13603, Level 16, State 1, Line 10
Property 't1..num' cannot be generated in JSON output due to invalid character in the column name or alias. Column name or alias that contains '..', starts or ends with '.' is not allowed in query that has FOR JSON clause.

如果我尝试用反斜杠转义点,则反斜杠被视为文字字符并且对象仍然是嵌套的:

[{"t1\\":{"num":1}},{"t1\\":{"num":2}},{"t1\\":{"num":3}},{"t1\\":{"num":4}},{"t1\\":{"num":5}},{"t1\\":{"num":6}},{"t1\\":{"num":7}},{"t1\\":{"num":8}},{"t1\\":{"num":9}},{"t1\\":{"num":10}}]

我如何实现我想要的目标?

1 个答案:

答案 0 :(得分:2)

这不是很好,但这是我的想法带给我的地方......我刚刚添加了replace(...,'_','.')

DECLARE @table1 TABLE (t1num int NOT NULL);
DECLARE @table2 TABLE (t2num int NOT NULL);
DECLARE @table3 TABLE (t3num int NOT NULL);

INSERT INTO @table1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table2 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table3 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);


SELECT
    t2num,
    replace((
        SELECT t1num AS [t1_num]
        FROM @table1
        FOR JSON PATH
    ),'_','.') AS t1s
FROM @table2 [t2]

<强>返回

t2num   t1s
1       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
2       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
3       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
4       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
5       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
6       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
7       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
8       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
9       [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
10      [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]

<强> dbFiddle