I am trying to retrieve some data in JSON, however, I am struggling to format it in the correct way, here an example:
CREATE TABLE #test (id int identity(1,1), name varchar(100), EntityType VARCHAR(10))
insert into #test values('Dell','PC')
insert into #test values('Microsoft','CO')
insert into #test values('MAC','PC')
insert into #test values('APPLE','CO')
SELECT * FROM #test WHERE EntityType = 'PC' FOR JSON PATH, ROOT('??')
drop table #test
I have been trying using root but unsuccessfully
I am looking for this result, it is an object and then an array group by a column
{
"CO": [
{
"id": 1,
"name": "Dell",
},
{
"id": 2,
"name": "Microsoft",
},
]
"PC" :[
{
"id": 3,
"name": "MAC",
},
{
"id": 4,
"name": "APPLE",
}]
}