I have json from another system, how to store in oracle database? I dont mind if using while or for or what ever, i only hope can store in database in 4 rows without null line as null line provided by another system. I already refer to many question but not find similar case data like mine.
This is then json example
{
"ErrorCode": "00",
"ErrorMessage": "",
"ServiceList": {
"ServiceDetail": [
{
"Service_name": [
"LTE_DATA_PRI_1",
"LTE_ONM_PRI_2"
],
"AVLAN": [
"LTE_DATA_PRI_SITE_1",
"LTE_DATA_PRI_SITE_2"
],
"IpAddress": [
"192.168.1/20",
"192.168.2/20"
],
"AggPort": [
"GigabitEthernet 1/1/10.1000",
"GigabitEthernet 1/1/10.1001"
]
},
"\r\n",
"\r\n",
{
"Service_name": [
"LTE_DATA_SEC_1",
"LTE_ONM_SEC_2"
],
"AVLAN": [
"LTE_DATA_SEC_SITE_1",
"LTE_DATA_SEC_SITE_2"
],
"IpAddress": [
"192.168.3/20",
"192.168.4/20"
],
"AggPort": [
"GigabitEthernet 1/1/10.1003",
"GigabitEthernet 1/1/10.1004"
]
},
"\r\n",
"\r\n"
]
}
}
expected display or store in database
below is my code in php file but it only save the primary
$i = 0;
$x = 0;
$arrayLength = count($array['ServiceList']['ServiceDetail']);
while ($x < $arrayLength)
{
$arrayLength2 = count($array['ServiceList']['ServiceDetail'][$x]['AVLAN']);
while ($i < $arrayLength2)
{
$AVLAN1 = $array['ServiceList']['ServiceDetail'][$x]['AVLAN'];
if ( $AVLAN1 !== null){
$p_avlan = $array['ServiceList']['ServiceDetail'][$x]['AVLAN'][$i];
$p_agg_port = $array['ServiceList']['ServiceDetail'][$x]['AggPort'][$i];
$p_ip_address = $array['ServiceList']['ServiceDetail'][$x]['IpAddress'][$i];
$p_service_name = $array['ServiceList']['ServiceDetail'][$x]['Service_name'][$i];
// sql procedure here //
$i++;
}
} // while 2
$x++;
} // while 1
Using foreach instead of while loops spares you from having to count entries and keeping your own loop index.
On the inner level, loop over the Service_name
entry, and then use the key of that loop to access the corresponding elements in the other arrays on the same level.
foreach($array['ServiceList']['ServiceDetail'] as $serviceDetail) {
if(is_array($serviceDetail)) {
foreach($serviceDetail['Service_name'] as $key => $p_service_name) {
$p_avlan = $serviceDetail['AVLAN'][$key];
$p_ip_address = $serviceDetail['IpAddress'][$key];
$p_agg_port = $serviceDetail['AggPort'][$key];
var_dump($p_service_name, $p_avlan, $p_ip_address, $p_agg_port);
}
}
}