Home > Mobile >  SQL Server table data to JSON Path result
SQL Server table data to JSON Path result

Time:04-01

I am looking for a solution to convert the table results to a JSON path.

I have a table with two columns as below. Column 1 Will always have normal values, but column 2 will have values up to 15 separated by ';' (semicolon).

ID   Column1               Column2
--------------------------------------
 1      T1               Re;BoRe;Va

I want to convert the above column data in to below JSON Format

{
    "services": 
    [
        { "service": "T1"}
    ], 
    "additional_services": 
    [
        { "service": "Re" }, 
        { "service": "BoRe" }, 
        { "service": "Va" }
    ]
}

I have tried creating something like the below, but cannot get to the exact format that I am looking for

SELECT 
    REPLACE((SELECT d.Column1 AS services, d.column2 AS additional_services
             FROM Table1 w (nolock)
             INNER JOIN Table2 d (nolock) ON w.Id = d.Id
             WHERE ID = 1
             FOR JSON PATH), '\/', '/')

Please let me know if this is something we can achieve using T-SQL

CodePudding user response:

As I mention in the comments, I strongly recommend you fix your design and normalise your design. Don't store delimited data in your database; Re;BoRe;Va should be 3 rows, not 1 delimited one. That doesn't mean you can't achieve what you want with your denormalised data, just that your design is flawed, and thus it needs being brought up.

One way to achieve what you're after is with some nested FOR JSON calls:

SELECT (SELECT V.Column1 AS service
        FOR JSON PATH) AS services,
        (SELECT SS.[value] AS service 
         FROM STRING_SPLIT(V.Column2,';') SS
         FOR JSON PATH) AS additional_services
FROM (VALUES(1,'T1','Re;BoRe;Va'))V(ID,Column1,Column2)
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

This results in the following JSON:

{
    "services": [
        {
            "service": "T1"
        }
    ],
    "additional_services": [
        {
            "service": "Re"
        },
        {
            "service": "BoRe"
        },
        {
            "service": "Va"
        }
    ]
}
  • Related