Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a dot/period in a column name with FOR JSON PATH without it creating a nested object?

Given the following script:

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]

The output for the t1s column will look like this:

[{"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}}]

However, I need it to look like this:

[{"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}]

...except with periods instead of underscores.

If I try to double-dot, SQL Server returns this error:

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.

And if I try to escape the dot with a backslash, the backslash is taken as a literal character and the object is still nested:

[{"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}}]

How can I accomplish what I want?

like image 909
tuespetre Avatar asked May 09 '17 13:05

tuespetre


People also ask

Can SQL column names have periods?

1. Only Use Lowercase Letters, Numbers, and Underscores. Don't use dots, spaces, or dashes in database, schema, table, or column names.

What is the query with used for to see JSON format?

Use the FOR JSON clause to simplify client applications by delegating the formatting of JSON output from the app to SQL Server. Azure Data Studio is the recommended query editor for JSON queries because it auto-formats the JSON results (as seen in this article) instead of displaying a flat string.

How do I query JSON data in SQL?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).


1 Answers

It's not elegent, but this is where my thinking takes me... I just added a 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]

Returns

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

like image 132
John Cappelletti Avatar answered Nov 09 '22 19:11

John Cappelletti