Luke Luke - 1 year ago 74
PHP Question

PHP read JSON file and output

I am trying to import a JSON file, read it and then output the results. I am a bit stuck with getting the JSON keys etc right.

This is an excerpt from the JSON file:

{
"CVE_data_type" : "CVE",
"CVE_data_format" : "MITRE",
"CVE_data_version" : "4.0",
"CVE_data_numberOfCVEs" : "566",
"CVE_data_timestamp" : "2017-07-26T10:00Z",
"CVE_Items" : [ {
"cve" : {
"CVE_data_meta" : {
"ID" : "CVE-2010-1154"
},
"affects" : {
"vendor" : {
"vendor_data" : [ ]
}
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ ]
} ]
},
"references" : {
"reference_data" : [ ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "** REJECT ** DO NOT USE THIS CANDIDATE NUMBER. ConsultIDs: none. Reason: This candidate was in a CNA pool that was not assigned to any issues during 2010. Notes: none."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ ]
},
"impact" : { },
"publishedDate" : "2017-07-24T16:29Z",
"lastModifiedDate" : "2017-07-24T16:29Z"
}, {
"cve" : {
"CVE_data_meta" : {
"ID" : "CVE-2010-1430"
},
"affects" : {
"vendor" : {
"vendor_data" : [ ]
}
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ ]
} ]
},
"references" : {
"reference_data" : [ ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "** REJECT ** DO NOT USE THIS CANDIDATE NUMBER. ConsultIDs: none. Reason: This candidate was in a CNA pool that was not assigned to any issues during 2010. Notes: none."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ ]
},
"impact" : { },
"publishedDate" : "2017-07-24T16:29Z",
"lastModifiedDate" : "2017-07-24T16:29Z"
}, {
"cve" : {
"CVE_data_meta" : {
"ID" : "CVE-2010-1631"
},
"affects" : {
"vendor" : {
"vendor_data" : [ ]
}
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ ]
} ]
},
"references" : {
"reference_data" : [ ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "** REJECT ** DO NOT USE THIS CANDIDATE NUMBER. ConsultIDs: none. Reason: This candidate was in a CNA pool that was not assigned to any issues during 2010. Notes: none."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ ]
},
"impact" : { },
"publishedDate" : "2017-07-24T16:29Z",
"lastModifiedDate" : "2017-07-24T16:29Z"
},


My PHP file is as follows:

<?php
//Load the JSON feed
$file = 'nvdcve-1.0-recent.json';
$json = file_get_contents($file);
$json_data = json_decode($json, true);
$vulns = $json_data->CVE_Items[0];

//Check if JSON file is actually found.
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}

//echo '<pre>' . var_dump($json_datas) . '</pre>';

//Loop through items and display
foreach ($vulns as $vuln) {
$cveid = $vuln->cve->CVE_data_meta->ID;
echo $cveid . '<br>';
}

//Print the array - Debugging only.
//print_r($json_datas);

?>


This PHP is just for testing, eventually, I will need to loop and echo out all the different sections to make a complete entry such as ID, affects, problemtype, references etc...

I've never used JSON with PHP before and I've read so many articles today, each telling me a different way of doing it, it's making my head hurt!

Answer Source

Your json is invalid. That's why json_decode($json) returns NULL. Check you json https://jsonformatter.curiousconcept.com/ here

UPDATE

<?php
$file = 'nvdcve-1.0-recent.json';
$json = file_get_contents($file);
$json_data = json_decode($json);
$vulns = $json_data->CVE_Items;

if (file_exists($file)) {
    echo "The file $file exists";
} else {
    echo "The file $file does not exist";
}
foreach ($vulns as $vuln) {
    $cveid = $vuln->cve->CVE_data_meta->ID;
    echo $cveid; echo '<br>';
}


?>

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download