I have a nested JSON variable(I use shortened version here) which I wanna insert into table. I am able to take not nested columns and values however struggling with nested part. What I want is retrieving 2 rows: one for sessionID = 20 and other one for 30 with sub columns. I am using SQL Server 2017.
DECLARE @json NVARCHAR(MAX)
SET @json =
N'{
"List":
[
{
"ID": 13,
"Date": "2015-12-07",
"SessionID": {
"20": {
"discount": "no",
"price": 15.99
},
"30": {
"discount": "yes",
"price": 12.99
}
}
}
]
}'
SELECT *
FROM OPENJSON(@json, N'lax $.List')
WITH (ID int '$.ID'
,[Date] date '$.Date'
,SessionID nvarchar(max) N'lax $.SessionID' AS JSON
)
I would like retrieve data as below. Thanks in advance for any help!

cross apply openjson(sessionid) as s cross apply openjson(s.value) with (discount nvarchar(3) '$.discount', price money '$.price') as v(where sessionid would be s.key)openjsonfunction. If you can change the source json to include the 20 and 30 as values, it would make your life much easier.