Consider I have below data,
create table #Temp(PropertyID nvarchar(255),BuildingID nvarchar(255),UnitID nvarchar(255),TenantName nvarchar(255),FieldName nvarchar(255),CurrentValue nvarchar(255),PreviousValue nvarchar(255))
insert into #Temp Values
('p1','B1','5','Spencer','Lease_EndDate','01/01/2021','03/01/2021'),
('p1','B1','5','Spencer','MonthlyBaseRent','3232','3000'),
('p1','B1','5','BCR','MonthlyBaseRent','1000','1100'),
('p1','B1','6','EA','MonthlyBaseRent','5000','5100'),
('p1','B2','5','VR','MonthlyBaseRent','3232','3000'),*
I need output as below nested JSON format, but I am getting flat JSON like [{},{},{}]
[
{
"PropertyID": "p1",
"Building": [
{
"BuildingID": "B1",
"Unit": [
{
"UnitID": "5",
"Tenant": [
{
"TenantName": "Spencer",
"Lease_EndDate": {
"CurrentValue": "01/01/2021",
"PreviousValue": "03/01/2021"
},
"MonthlyBaseRent": {
"CurrentValue": "3232",
"PreviousValue": "3000"
}
},
{
"TenantName": "BCR",
"MonthlyBaseRent": {
"CurrentValue": "1000",
"PreviousValue": "1100"
}
}
]
},
{
"UnitID": "6",
"Tenant": [
{
"TenantName": "EA",
"MonthlyBaseRent": {
"CurrentValue": "5000",
"PreviousValue": "5100"
}
}
]
}
]
},
{
"BuildingID": "B2",
"Unit": [
{
"UnitID": "5",
"Tenant": [
{
"TenantName": "VR",
"MonthlyBaseRent": {
"CurrentValue": "3232",
"PreviousValue": "3000"
}
}
]
}
]
}
]
}
]