apps table
app_id | app_name | app_status
-------------------------------------
1 scheduling INACTIVE
2 call ACTIVE
3 billing ACTIVE
4 order ACTIVE
I have a query,
SELECT * FROM apps WHERE app_status = "ACTIVE" FOR JSON PATH;
This query has an app_id field. I also want a field to accompany the json output with a field ids with all the app_id as a list.
So instead of:
[{
"app_id": 2,
"app_name": "call",
"status": "ACTIVE"
}, {
"app_id": 3,
"app_name": "billing",
"status": "ACTIVE"
}, {
"app_id": 4,
"app_name": "order",
"status": "ACTIVE"
}]
I would have:
[{
"apps": [{
"app_id": 2,
"app_name": "call",
"status": "ACTIVE"
}, {
"app_id": 3,
"app_name": "billing",
"status": "ACTIVE"
}, {
"app_id": 4,
"app_name": "order",
"status": "ACTIVE"
}],
"ids": [2, 3, 4]
}]
JSON_QUERY( STUFF( ... FOR XML PATH('') ) )is popular, such as this previous StackOverflow Answer.