Following is the given JSON data:
DECLARE @Jdata NVARCHAR(MAX) =
'{
"EmployeeDetails": {
"BusinessEntityID": 3,
"NationalIDNumber": 509647174,
"JobTitle": "Engineering Manager",
"BirthDate": "1974-11-12",
"MaritalStatus": "M",
"Gender": "M",
"StoreDetail": {
"Store": [
{
"AnnualSales": 800000,
"AnnualRevenue": 80000,
"BankName": "Guardian Bank",
"BusinessType": "BM",
"YearOpened": 1987,
"Specialty": "Touring",
"SquareFeet": 21000
},
{
"AnnualSales": 300000,
"AnnualRevenue": 30000,
"BankName": "International Bank",
"BusinessType": "BM",
"YearOpened": 1982,
"Specialty": "Road",
"SquareFeet": 9000
}
]
}
}
}';
Need to display:
BusinessEntityID | AnnualSales | BusinessType
-------------------------------------------------
3 300000 BM
3 800000 BM
My try:
select *
from OPENJSON(@jdata)
WITH(
BusinessEntityID VARCHAR(20) '$.EmployeeDetails.BusinessEntityID',
AnnualSales integer '$.EmployeeDetails.StoreDetail.Store.AnnualSales',
BusinessType VARCHAR(100) '$.EmployeeDetails.StoreDetail.Store.BusinessType'
) as a
But getting wrong output.
BusinessEntityID | AnnualSales | BusinessType
-------------------------------------------------
3 NULL NULL