Dynamic how to execute using nested objects, arrays

Working as expected.
i have using this body of JSON passing to JavaScript its insert into SQL table.
Input

data : 
[{
    "name": "John",
    "detail" : "contact",
    "type" : "phone",
    "value"  : "987-654-3210"
},
{    "name": "John",
    "detail" : "contact",
    "type" : "email",
    "value"  : "[email protected]"
},
{    "name": "John",
    "detail" : "address",
    "type" : "city",
    "value"  : "Berlin"
},
{   "name": "John",
    "detail" : "address",
    "type" : "country",
    "value"  : "Germany"
}]

Javascript Process

var data;
var rawdata =  JSON.parse(JSON.stringify(data));

var query= ""

arrayLength = rawdata.length;

for (var i = 0; i < arrayLength; i++) { 

query +=` begin
          insert into [dbname].[dbo].[table] (name,detail,type,value) 
         values('` + rawdata[i].name+ `','` + rawdata[i].detail + `','` + rawdata[i].type+ `','` + 
             '` + rawdata[i].value + `')
            end       
}
return query;

SQL table Output

| name     | detail   |type     |value           |
| -------- | -------- |---------|----------------|
| John     | contact  |phone    |987-654-3210    |
| John     | contact  |email    |[email protected] |
| John     | address  |city     |Berlin          |
| John     | address  |country  |Germany         |

Below i have this body of JSON.
How to pass to JavaScript function to insert into SQL table?

Input

{
    "data": [{
        "name": "John",
        "contact": {
            "phone": "987-654-3210",
            "email": "[email protected]"
        },
        "address": {
            "city": "Berlin",
            "country": "Germany"
        }
    }]
}

SQL table Output

| name     | detail   |type     |value           |
| -------- | -------- |---------|----------------|
| John     | contact  |phone    |987-654-3210    |
| John     | contact  |email    |[email protected] |
| John     | address  |city     |Berlin          |
| John     | address  |country  |Germany         |

How to create JavaScript function ? Any suggestion on this?