I am trying to map a json file to a mysql database see undefined index Name for code
the problem I believe is that there is no value for either element from the 3rd entry but there is a value called aws:autoscaling:groupName
I am just wondering how I should go about declaring this is it the same or does the colons make a difference as using the method in the aforementioned question fails due to not finding index as per the previous question thanks
my json or at least the important bits
{
"fileVersion":"1.0",
"configurationItems":[
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"",
"availabilityZone":"",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-01-06T10:37:37.000Z",
"tags":{
"Name":"dbn.prod-us.wordeo.com",
"cirrushq_id":"instance_20"
},
"relationships":[ ],
"configuration":{ }
},
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"",
"availabilityZone":"",
"configurationStateMd5Hash":"",
"resourceType":"",
"resourceCreationTime":"",
"tags":{
"Name":"db-backup.prod-us.wordeo.com",
"cirrushq_id":"instance_7701"
},
"relationships":[ ],
"configuration":{ }
},
{ },
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"",
"availabilityZone":"",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-09-29T07:25:44.000Z",
"tags":{
"aws:autoscaling:groupName":"ESND-PROD-US-14-02-14"
},
"relationships":[ ],
and here is the php
<?php
$con=mysqli_connect("localhost","root","","json_map");
$response = array();
$res=array();
$json = file_get_contents('C:\Users\Richard\Desktop\test.json');
if($json!=null){
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$Name=$configurationItems["tags"]["Name"];
echo "Name:",$Name,"<br />";
$cirrushq_id=$configurationItems["tags"]["cirrushq_id"];
echo "cirrushq_id:",$cirrushq_id,"<br />";
$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id) VALUES('$Name','$cirrushq_id')")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
}// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored tags ";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>
from the 3rd entry in tags down there is no name or id only
aws:autoscaling:groupName":"ESND-PROD-US-14-02-14"
I added this line to my php
$awsautoscalinggroupName= $configurationItems["tags"] ["aws:autoscaling:groupName"];