0

I am trying to get data from json file , parse it and add data from it to database. I am using as database, oracle. I am getting the errors :

enter image description here

This is my JSON file

{
"data": {
    "gps": [
        { "location": "45.778123,24.151165"     
        },
        { "location": "45.7875116,24.1549801"               
        }
    ]
}

This is my php file

    <?php
        $file = 'JSONdata.json';
        $json = file_get_contents($file,0,null,null);
        $obj = json_decode($json,true);
        $data = $obj->data->gps;

        include 'connection.php';

       function insert_data($connname, $conn)
       {
          foreach ($data as $post) {
             $stmt = oci_parse($conn, "insert into GPSLOGS
                   values('".$post->location."')");
            oci_execute($stmt, OCI_DEFAULT);
            echo "$connname inserted row without committing<br>\n";
            }
       }

        insert_data('c1', $conn);
   ?>

Do you know what am I doing wrong? And why it keeps getting those errors? I also tried using another format of JSON file and parse it differently , but it keeps getting the same errors.

.json
      [{ "gps": "45.778123,24.151165"},{"gps": "45.7875116,24.1549801"}]

 .php
     $file = 'JSONdata.json';
     $json = file_get_contents($file,0,null,null);
     $obj = json_decode($json,true);    
       foreach($obj as $item)
       {      echo $item['gps'];}  

1 Answer 1

2
$obj = json_decode($json,true);
$data = $obj->data->gps;

returns data in a form of array, not object. Remove the 2nd argument from json decode and everything should work.

make it

$obj = json_decode($json);
$data = $obj->data->gps;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.