I have a stored procedure in SQL Server 2017 that outputs a result set as JSON. It works to output the set just fine, but I want it in a different format.
According to the MS documentation, the format it's sending out is as expected, but it seems counter-intuitive to me. I'm not JSON expert by any means but I've always assumed it to be a single object representation of a structure.
The query is:
SELECT
e.EventID AS 'Event.ID',
EventDate AS 'Event.Date',
ea.ActivityID AS 'Event.Activity.ID',
ea.CreateDate AS 'Event.Activity.Date',
ea.Notes AS 'Event.Activity.Notes'
FROM
Events e
JOIN
dbo.EventActivities ea ON e.EventID = ea.EventID
FOR JSON PATH
This returns an output of:
[
{"Event": {
"ID":"236",
"Date":"2019-03-01",
"Activity": {"ID": 10,
"Date":"2019-01-02T11:47:33.2800000",
"Notes":"Event created"}
}
},
{"Event": {
"ID":"236",
"Date":"2019-03-01",
"Activity": {"ID":20,
"Date":"2019-01-02T11:47:34.3933333",
"Notes":"Staff selected"}
}
},
{"Event": {
"ID":"236",
"Date":"2019-03-01",
"Activity": {"ID":20,
"Date":"2019-01-02T11:47:34.3933333",
"Notes":"Staff selected"}
}
}
]
When I format this manually (to visualise it better, it's giving me an array of 3 identical events, for each activity. This is consistent with what MS say in Format Nested JSON
I was expecting (or rather hoping) to see something like:
[
{
"Event": {
"ID": "236",
"Date": "2019-03-01",
"Activity": [
{
"ID": 10,
"Date": "2019-01-02T11:47:33.2800000",
"Notes": "Event created"
},
{
"ID": 20,
"Date": "2019-01-02T11:47:34.3933333",
"Notes": "Staff selected"
},
{
"ID": 20,
"Date": "2019-01-02T11:47:34.3933333",
"Notes": "Staff selected"
}
]
}
}
]
Is it possible to get an output formulated like this? or would this be invalid?