I'd like to get JSON with an array of integers using SQL Server 2016's For JSON
feature. I'm stumped on array of integers.
Database table structures:
declare @Employees table (ID int, Name nvarchar(50))
insert into @Employees values
(1, 'Bob'),
(2, 'Randy')
declare @Permissions table (ID int, PermissionName nvarchar(50))
insert into @Permissions values
(1, 'Post'),
(2, 'Comment'),
(3, 'Edit'),
(4, 'Delete')
declare @EmployeePermissions table (EmployeeID int, PermissionID int)
insert into @EmployeePermissions values
(1, 1),
(1, 2),
(2, 1),
(2, 2),
(2, 3)
Desired results:
{"EmployeePermissions": [
{"Employee":"Bob", "Permissions":[1,2]},
{"Employee":"Randy", "Permissions":[1,2,3]}
]}
This is the closest I've gotten, but not quite what I want.
select
e.Name as Employee,
(select
convert(nvarchar(10),ep.PermissionID) as PermID
from @EmployeePermissions ep
where ep.EmployeeID=e.ID
for json path) as 'Permissions'
from
@Employees e
for json path, root('EmployeePermissions')
returns:
{"EmployeePermissions": [
{"Employee":"Bob", "Permissions":[{"permID":1},{"permID":2}]},
{"Employee":"Randy", "Permissions":[{"permID":1},{"permID":2},{"permID":3}]}
]}
SOLUTION - SQL Server 2017 and on
select
e.Name as Employee,
(select
'[' + STRING_AGG(ep.PermissionID, ',') + ']'
from @EmployeePermissions ep
where ep.EmployeeID=e.ID) as Permissions
from
@Employees e
for json path, root('EmployeePermissions')
SQL Server
I suggest you let the consumer know about these aliases. If you're usingJson.NET
, then see this excellent answer: https://mcmap.net/q/830506/-name-array-elements-in-json-net – Nonpayment