Hi @kasim mohamed ,
To retrieve values from a JSON there are several built-in functions you may use. Check this link for more information.
To get you started, here are some examples, I also added a corrected json (there were some quotes missing).
DECLARE @json NVARCHAR(MAX)
SET @json =
N'[{
"HTable": {
"HID": "H1001",
"HDate": "22/08/2023"
},
"CTable": {
"ID": "C1001",
"Amount": 200,
"ETable": {
"EID": "E1001",
"EType": "EType1"
},
"DTable": [
{
"ID": "D1001",
"Code": "DType1"
},
{
"ID": "D1002",
"Code": "DType2"
}
],
"ATable": [
{
"ID": "A1001",
"Code": "ACode1",
"OTable": {
"ID": "O1001",
"Type": "OType1"
},
"OTable": {
"ID": "O1002",
"Type": "OType2"
}
},
{
"ID": "A1002",
"Code": "ACode2"
}
]
}
}
]'
When you just want a single value and do not have a JSON array you can use:
INSERT INTO ##HTable (HID )
SELECT JSON_VALUE(value,'$.HTable.HID')
FROM OPENJSON(@json)
When you are working with arrays, you will come up with something like this:
INSERT INTO ##DTable (CID)
SELECT JSON_VALUE(DTable.value,'$.ID')
FROM OPENJSON(@json)
CROSS APPLY OPENJSON(value,'$') main
CROSS APPLY OPENJSON(main.value,'$.DTable') DTable
Hope this will help you.
Kind regards,
Wilko
Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members. If you have extra questions about this answer, please click "Comment".