Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query to JSON

Hi I am new to JSON and would like to ask this question:

So my goal is trying to make the result look like this:

{
    "userid": "10293304" ,     "segments": ["Online_Flag"]
} 

{
    "userid": "10292298" , "segments": ["schedule_Appointment", "Enrolled_Order","Complete_Order"]

}

I was able to use T SQL to query the result to look like this:

[{"userid":"10293159","segments":[{"segment":"Schedule_Appointment"}]},
{"userid":"10293056","segments":[{"segment":"Schedule_Appointment"}]},
{"userid":"10292838","segments":[{"segment":"Schedule_Appointment"}]},

The issue is that I only need to show the JSON value (Schedule_Appointment), not the key(segment)

is there any way (using TSQL) to hide the keys and only show the values in JSON Array?

I have tried to look for ways to create JSON array using SQL but no luck, if anyone can explain I would really appreciate.

SQL SERVER 2016

Data

CREATE TABLE [dbo].[Lotame_JSON](
[lead_id] [varchar](100) NULL,
[dist_date_key] [date] NULL,
[online_flag] [int] NULL,
[sched_appt_qty] [int] NULL,
[enroll_order_qty] [int] NULL,
[compl_order_qty] [int] NULL)

insert into [dbo].[Lotame_JSON]
values('105646','2016-12-1',1,0,0,1)

insert into [dbo].[Lotame_JSON]
values('125646','2016-12-1',0,0,1,0)

insert into [dbo].[Lotame_JSON]
values('112646','2016-12-1',0,1,1,0)

insert into [dbo].[Lotame_JSON]
values('106446','2016-12-1',0,0,1,0)

Query I have:

select 
[lead_id] AS 'userid',
(
SELECT segment
    from 
    (
        SELECT              
            CASE WHEN [online_flag] > 0 THEN '1' else null end as 'online_flag',
            CASE WHEN sched_appt_qty > 0 THEN '2' else null end as 'Schedule_Appointment',
            CASE WHEN enroll_order_qty > 0 THEN '3'  else null end as 'Enrolled_Order',
            CASE WHEN compl_order_qty > 0 THEN '4'  else null end as 'Complete_Order'
        FROM [dbo].[Lotame_JSON] as sub
        WHERE sub.lead_id = main.lead_id
    ) t
    UNPIVOT
    (
        segment1
        for segment in (online_flag,Schedule_Appointment,Enrolled_Order,Complete_Order)
    ) as UnPvot
    for JSON PATH
) AS 'segments' 
from [dbo].[Lotame_JSON] as main
where online_flag = 0 and (sched_appt_qty > 0 or enroll_order_qty > 0   or    compl_order_qty > 0)
and dist_date_key >= '2016-9-1'
FOR JSON PATH

Thanks

like image 239
nghoang Avatar asked Dec 13 '25 14:12

nghoang


1 Answers

You're very close. There's a trick to getting JSON arrays formatted correctly in SQL. You use a combination of JSON_QUERY, STUFF, and FOR XML PATH on the data you want in the array. And to get results without the outer-most square brackets, use FOR JSON PATH, WITHOUT_ARRAY_WRAPPER. Put it all together and you get a query that looks like this:

SELECT 
userid = [lead_id]
,segments = JSON_QUERY(
                        '[' + STUFF(
                                    (
                                        SELECT ',' + '"' +  segment + '"'
                                        FROM
                                        (
                                            SELECT              
                                                CASE WHEN [online_flag] > 0 THEN '1' ELSE NULL END AS 'online_flag',
                                                CASE WHEN sched_appt_qty > 0 THEN '2' ELSE NULL END AS 'Schedule_Appointment',
                                                CASE WHEN enroll_order_qty > 0 THEN '3' ELSE NULL END AS 'Enrolled_Order',
                                                CASE WHEN compl_order_qty > 0 THEN '4' ELSE NULL END AS 'Complete_Order'
                                            FROM [dbo].[Lotame_JSON] AS sub
                                            WHERE sub.lead_id = main.lead_id
                                        ) t
                                        UNPIVOT
                                        (
                                            segment1 FOR segment IN (online_flag, Schedule_Appointment, Enrolled_Order, Complete_Order)
                                        ) AS UnPvot
                                        FOR XML PATH ('')
                                    )
                        , 1, 1, '') + ']'
                    )
FROM [dbo].[Lotame_JSON] AS main
WHERE online_flag = 0
    AND dist_date_key >= '2016-9-1'
    AND (
            sched_appt_qty > 0 
            OR enroll_order_qty > 0
            OR compl_order_qty > 0
        )
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
like image 109
digital.aaron Avatar answered Dec 15 '25 13:12

digital.aaron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!