Hi @Raj D
For JSON string with Nested array of elements, you could use JSON_Value() together with OPENJSON() function. Like this:
DECLARE @json NVARCHAR(max)
SELECT @json =
N'{
"Data": [
{
"name": "ABC",
"date": "2020-01-20",
"subproject": [
{
"id": "123",
"projectname": "new1",
"refnum": "123:new1"
},
{
"id": "456",
"projectname": "new2",
"refnum": "456:new2"
}
],
"projectid": "1234",
"projectdate": "2020-01-27"
},
{
"name": "DEF",
"date": "2020-01-30",
"subproject": [
{
"id": "789",
"projectname": "new3",
"refnum": "789:new3"
},
{
"id": "901",
"projectname": "new4",
"refnum": "901:new4"
}
],
"projectid": "4567",
"projectdate": "2020-02-07"
},
{
"name": "HGI",
"date": "2019-03-31",
"subproject": [
{
"id": "327",
"projectname": "new8",
"refnum": "327:new8"
},
{
"id": "203",
"projectname": "new24",
"refnum": "203:new24",
"modifieddate": "2022-03-07"
}
],
"projectid": "9281",
"projectdate": "2020-04-10",
"projectmodifieddate": "2022-03-07"
},
{
"name": "IKL",
"date": "2019-05-30",
"subproject": [
{
"id": "308",
"projectname": "new28",
"refnum": "308:new28"
},
{
"id": "113",
"projectname": "new31",
"refnum": "113:new31",
"modifieddate": "2022-03-07"
}
],
"projectid": "7324",
"projectdate": "2020-05-31",
"projectmodifieddate": "2022-03-07",
"projectdescription": "This is a test project"
}
]
}'
SELECT
JSON_Value (p.value, '$.name') as [name],
JSON_Value (p.value, '$.date') as [date],
JSON_Value (s.value, '$.id') as subproject_id,
JSON_Value (s.value, '$.projectname') as subproject_projectname,
JSON_Value (s.value, '$.refnum') as subproject_refnum,
JSON_Value (s.value, '$.modifieddate') as subproject_modifieddate,
JSON_Value (p.value, '$.projectid') as projectid,
JSON_Value (p.value, '$.projectdate') as projectdate,
JSON_Value (p.value, '$.projectmodifieddate') as projectmodifieddate,
JSON_Value (p.value, '$.projectdescription') as projectdescription
FROM OPENJSON (@json, '$.Data') as p
CROSS APPLY OPENJSON (p.value, '$.subproject') as s
Refer to this article for more details and methods: Reading JSON string with Nested array of elements
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.