1

Hi I am working on trying to strip the contents of a json file and map to a mysql database structure but am having an issue with the "tags" values in that the value Name is not found yet still populates 2 entries in the database in the right place

here is a sample of the json :

{  
"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":"i-0f8a032c",
     "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); 
            } 

        } 
} 



?> 
Richard Hewitt
  • 345
  • 1
  • 5
  • 22
  • You have several unmatched quotes in your JSON. Are you sure you copied it correctly? – Barmar Jan 19 '15 at 10:12
  • @Barmar json file could contain info not for public eyes so I took out several values I probably edited hastily will edit in a sec – Richard Hewitt Jan 19 '15 at 10:16
  • What do you see if you `var_dump($configurationItems['tags'])`? – Barmar Jan 19 '15 at 10:17
  • @Barmar I am being really thick here where would I put that as you can tell I am a complete newbie thanks ! – Richard Hewitt Jan 19 '15 at 11:24
  • Put it inside the `foreach` loop. – Barmar Jan 19 '15 at 11:25
  • @Barmar thanks I have tried that but keep getting unexpected end of file see [ http://stackoverflow.com/questions/28024543/how-to-use-var-dump-function ] one thing I can tell you is I modified the script to be just the cirrushq_id but it fails to find that either its very odd because I have populated other siblings like this no problem thanks – Richard Hewitt Jan 19 '15 at 16:01
  • You probably left out a semicolon or something like that when you added it. If you're having trouble with basic syntax like this, it's going to be hard to figure out what's going on. And if you sanitize things when you copy it here, you may be losing the problem when copying. – Barmar Jan 19 '15 at 16:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69143/discussion-between-richard-hewitt-and-barmar). – Richard Hewitt Jan 19 '15 at 16:27

1 Answers1

1

You php code works just fine, but your json is not valid for instance :

"configurationStateId":, => double quote missing.

Use double quote instead of single quote :

"ARN":", => ""

http://jsonlint.com/

JC Sama
  • 2,214
  • 1
  • 13
  • 13