How to parse JSON with PHP

Hello ,
  I am reading the content from a webpage and data is return in json. How do I extract the individual value ... e.g

index =1, IP =172.1.1.1
index 2, IP =172.2.2.2

This is the json output

e.g
{"DATA":
 [
  {"Index":"1","0":"1","IP":"17.1.1.1","1":"172.1.1.1","DN":"1561","2":"1561923","CallID":"3fbeed66bc10ac0e","3":"3fbeed66bc10ac0e"},
  {"Index":"2","2":"1","IP":"17.2.2.2","1":"172.1.1.1","DN":"1212","2":"1561923","CallID":"3fbeed66bc10ac0e","3":"3fbeed66bc10ac0e"}
 ]
}

Open in new window

SiemensSENAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
PHP has a built-in as of PHP 5.2:
http://php.net/manual/en/function.json-decode.php
0
SiemensSENAuthor Commented:
Thanks, I tried it before posting . The error is "Fatal error:Object of class stdClass could not be converted to a string on line 19.

I thing the error  is because of  the command
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

Open in new window

How do I prevent curl from printing the result when I don't want it??

------------------------------


Here my code

$ch = curl_init();
  $url ="https://172.20.32.1:28081//public/index.php/api/index";
    $data = array(
    "qid" => "2",
    "days" => "5",
    "sys" => "tracemanager",);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, "", '&'));
    $response= curl_exec($ch);
    curl_close($ch);
    echo json_decode($response);
0
SiemensSENAuthor Commented:
Also, the json_decode return 1

Thanks for your help
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Chris StanyonWebDevCommented:
We can't test that code at all. We don't have access to the URL (it times out) and an error on line 19 is meaningless when you've only posted 15 lines of code.

Is this question about decoding JSON data to gain access to the information, or about making a cURL request??
0
SiemensSENAuthor Commented:
I format the code .. line 19 is last line  

echo json_decode($response); 

Open in new window


I am try to parse the output. The URL is an internal link and I include the  code just  to see anyone can identify the  error in the few line of code.

Here a few line of data that is return when I enter the URL on the browser

{"DATA":
[
{"Index":"1","IP":"172.20.32.7"},
{"Index":"2","IP":"172.20.32.7",}
])

I would like to use PHP to get the data and parse it.

Thanks for your help
0
Chris StanyonWebDevCommented:
OK. Assuming your cURL request is returning valid JSON data, you will need to use the json_decode function to create an object. This object will contain a property called DATA, which will in turn contain an array of objects - each one representing a row from your returned data. You can then access the properties of each object by looping through the array:

<?php
$response= curl_exec($ch);
$JSONData = json_decode($response);
foreach ($JSONData->DATA as $info) {
	var_dump($info); //look at all the info
	//access individual properties
	echo "Index is: " . $info->Index;
	echo "IP is: " . $info->IP;
	echo "CallID is: " . $info->CallID;
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
This is not valid JSON:
{"DATA":
[
{"Index":"1","IP":"172.20.32.7"},
{"Index":"2","IP":"172.20.32.7",}
])

Suggest you contact the publisher and ask if they have a different API or if they can fix the JSON string.  Details about JSON are here:
http://www.json.org/
0
SiemensSENAuthor Commented:
Thanks, This is the JSON. I edited the file because it is hugh.  


Corrected file:
{
   "DATA":[
      {
         "Index":"1",
         "IP":"172.20.32.7"
      },
      {
         "Index":"2",
         "IP":"172.20.32.7"
      }
   ]
}

Also, I am generating the JSON. It is simply reading  a data from a SQLITE db and then I would like to consume it using php CURL

Server code snippet
----------------------------
$dbh = new PDO("sqlite:".$dbname,"","");
$stmt = $dbh->prepare($qry);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$result = $stmt->fetchAll();
$retV = array("DATA"=>$result);
echo json_encode($result); 

Open in new window




Then on a remove machine, I am trying to use CURL to get the data and display it

<?php

   $ch = curl_init();
    $url ="https://myurl"; 
    $data = array(
    "qid" => "2", 
    "days" => "5",
    "sys" => "tracemanager",);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, "", '&'));
    $response= curl_exec($ch);
    $JSONData = json_decode($response,true);
    foreach ($JSONData->DATA as $info) 
    {
	var_dump($info); 
    }
    curl_close($ch);
?>

Open in new window


The above code does not work , the line
$JSONData = json_decode($response,true);

Open in new window

return 1
0
Chris StanyonWebDevCommented:
Right.. Before you try and decode the data, just var_dump it to make sure you're definitely getting back the data properly.

Also, in the code above, you've added true to the json_decode call, so you're are asking for an array, not objects. This means that $JSONData->DATA won't work - you need to use $JSONData['DATA'] - if you want objects, drop the true :

$JSONData = json_decode($response);
0
Ray PaseurCommented:
Here is your new best friend in the PHP language!  No need to ever wonder about the content of a variable again!
http://php.net/manual/en/function.var-dump.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.