Vishal Kinjavdekar Vishal Kinjavdekar - 1 month ago 18
JSON Question

Decode nested JSON PHP

I want to get the data such as domain name, dns, ip address of a website and I have found the following JSON structure that can help me to do so.
But I am getting errors in parsing particular information such as dns_records->type , dns_records->target etc.


Here is my nested JSON code:

{
"meta": {
"code": 200,
"time": 1477428559,
"requestId": "1f1d72299d7138924196681ca628a699"
},
"data": {
"tutorialspoint.com": {
"updated_datetime": "2015-08-17 13:56:03",
"domain_status": "[\"clientDeleteProhibited http:\\/\\/www.icann.org\\/epp#clientDeleteProhibited\",\"clientRenewProhibited http:\\/\\/www.icann.org\\/epp#clientRenewProhibited\",\"clientTransferProhibited http:\\/\\/www.icann.org\\/epp#clientTransferProhibited\",\"clientUpdateProhibited http:\\/\\/www.icann.org\\/epp#clientUpdateProhibited\"]",
"full_whois": "Domain Name: TUTORIALSPOINT.COM",
"dns_records": "[{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"A\",\"ip\":\"66.135.33.172\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns44.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns43.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"SOA\",\"mname\":\"ns43.domaincontrol.com\",\"rname\":\"dns.jomax.net\",\"serial\":2015080700,\"refresh\":28800,\"retry\":7200,\"expire\":604800,\"minimum-ttl\":3600},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":1,\"target\":\"ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT1.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT2.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT3.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT4.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"v=spf1 mx include:_spf.google.com -all\",\"entries\":[\"v=spf1 mx include:_spf.google.com -all\"]},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\",\"entries\":[\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\"]}]",
"owner_email": null,
"registrar": "GODADDY.COM, LLC",
"registrar_slug": "godaddy.com-llc",
"hosted_ip": "66.135.33.172",
"hosted_ip_latitude": "29.4997",
"hosted_ip_longitude": "-98.3992",
"hosted_ip_country": "US",
"hosted_ip_friendly_location": "Texas, San Antonio, United States, 78218",
"hosted_ip_full_country": "United States"
}
}
}


How do I find the
dns_records->type
and
dns_records->target
from the above code?

Answer

dns_records contain more type and target so you can access all types and targets this way,

$record = '{
  "meta": {
    "code": 200,
    "time": 1477428559,
    "requestId": "1f1d72299d7138924196681ca628a699"
  },
  "data": {
    "tutorialspoint.com": {
      "updated_datetime": "2015-08-17 13:56:03",
      "domain_status": "[\"clientDeleteProhibited http:\\/\\/www.icann.org\\/epp#clientDeleteProhibited\",\"clientRenewProhibited http:\\/\\/www.icann.org\\/epp#clientRenewProhibited\",\"clientTransferProhibited http:\\/\\/www.icann.org\\/epp#clientTransferProhibited\",\"clientUpdateProhibited http:\\/\\/www.icann.org\\/epp#clientUpdateProhibited\"]",
      "full_whois": "Domain Name: TUTORIALSPOINT.COM",
      "dns_records": "[{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"A\",\"ip\":\"66.135.33.172\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns44.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns43.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"SOA\",\"mname\":\"ns43.domaincontrol.com\",\"rname\":\"dns.jomax.net\",\"serial\":2015080700,\"refresh\":28800,\"retry\":7200,\"expire\":604800,\"minimum-ttl\":3600},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":1,\"target\":\"ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT1.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT2.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT3.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT4.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"v=spf1 mx include:_spf.google.com -all\",\"entries\":[\"v=spf1 mx include:_spf.google.com -all\"]},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\",\"entries\":[\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\"]}]",
      "owner_email": null,
      "registrar": "GODADDY.COM, LLC",
      "registrar_slug": "godaddy.com-llc",
      "hosted_ip": "66.135.33.172",
      "hosted_ip_latitude": "29.4997",
      "hosted_ip_longitude": "-98.3992",
      "hosted_ip_country": "US",
      "hosted_ip_friendly_location": "Texas, San Antonio, United States, 78218",
      "hosted_ip_full_country": "United States"
    }
  }
}';


$record = json_decode($record, true);

$dnsRecords = json_decode($record['data']['tutorialspoint.com']['dns_records'], true);

$typeArray = array();
$targetArray = array();
 foreach($dnsRecords as $row){
    foreach($row as $key=>$res){
        if($key == 'type'){
            $typeArray[] = $res;
        }
        if($key == 'target'){
            $targetArray[] = $res;
        }
    }
}

I created two arrays for targets and types, you can access each target and type this way,

// targets
foreach($targetArray as $target){
    echo $target;
}

// types
foreach($typeArray as $type){
    echo $type;
}