Q: Howto parse JSON with PHP5?

I would like to process the result of a JSON request in PHP5. But json_decode() returns NULL. Does anybody have small example?

Here is my (not working) code:

<?php
# An HTTP POST request example

$url="http://localhost:8080/rest/things/sensor%33%3Atemperatures";

$handle = curl_init();
 
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
// Set the result output to be a string.
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, true);
 
$output = curl_exec($handle); 
$responseCode   = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if(curl_errno($handle))
{
  print curl_error($handle);
}
else
{
  if($responseCode == "200") echo "successful request";
}

curl_close($handle);

$json= json_decode($output,$assoc=true);
print $json;
var_dump(json_decode($json,true));
echo "<br/>".$json["statusInfo"]."<br/>";
?>

This returns:
successful requestNULL

Maybe someone can help me. Thanks in advance for your answers,
Christof

I’m on mobile now and cannot test anything, but you can start to echo the $output variable to see if it gets populated in the first place.
Another thing is this:

Should be :

 $json= json_decode($output,true)

Given this is an openHAB forum and openHAB is not written in PHP nor does it use PHP this post is most likely more appropriate for some other forum.

1 Like

This is WAY off-topic.
I do use PHP but I actually use command line cURL within PHP rather than their monstrosity.

Perhaps $output is not just JSON. Of course, you can use print_r() to display that variable. I assume you have already done that?

Yes, indeed it’s off-topic but I thought there is already somebody who knows how to do it with PHP.

Yes, I did. I am not so in the JSON topic, so I cannot say if it’s correct syntax:

successful request
HTTP/1.1 200 OK Content-Type: application/json Content-Length: 2671 Server: Jetty(9.4.11.v20180605) {"statusInfo":{"status":"ONLINE","statusDetail":"NONE"},"editable":true,"label":"temperatures_heizung","bridgeUID":"km200:kmdevice:9572","configuration":{},"properties":{"root":"#system#sensors#temperatures"},"UID":"km200:sensor:9572:temperatures","thingTypeUID":"km200:sensor","channels":[{"linkedItems":["T_Aussen_Hz","km200_sensor_9572_temperatures_outdoor_t1"],"uid":"km200:sensor:9572:temperatures:outdoor_t1","id":"outdoor_t1","channelTypeUID":"km200:sensor:9572:temperatures:outdoor_t1","itemType":"Number","kind":"STATE","label":"outdoor_t1","description":"outdoor_t1","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#outdoor_t1"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_chimney"],"uid":"km200:sensor:9572:temperatures:chimney","id":"chimney","channelTypeUID":"km200:sensor:9572:temperatures:chimney","itemType":"Number","kind":"STATE","label":"chimney","description":"chimney","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#chimney"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_supply_t1_setpoint"],"uid":"km200:sensor:9572:temperatures:supply_t1_setpoint","id":"supply_t1_setpoint","channelTypeUID":"km200:sensor:9572:temperatures:supply_t1_setpoint","itemType":"Number","kind":"STATE","label":"supply_t1_setpoint","description":"supply_t1_setpoint","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#supply_t1_setpoint"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_supply_t1"],"uid":"km200:sensor:9572:temperatures:supply_t1","id":"supply_t1","channelTypeUID":"km200:sensor:9572:temperatures:supply_t1","itemType":"Number","kind":"STATE","label":"supply_t1","description":"supply_t1","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#supply_t1"},"configuration":{}},{"linkedItems":["T_Warmwasser","km200_sensor_9572_temperatures_hotWater_t1"],"uid":"km200:sensor:9572:temperatures:hotWater_t1","id":"hotWater_t1","channelTypeUID":"km200:sensor:9572:temperatures:hotWater_t1","itemType":"Number","kind":"STATE","label":"hotWater_t1","description":"hotWater_t1","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#hotWater_t1"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_switch"],"uid":"km200:sensor:9572:temperatures:switch","id":"switch","channelTypeUID":"km200:sensor:9572:temperatures:switch","itemType":"Number","kind":"STATE","label":"switch","description":"switch","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#switch"},"configuration":{}}]} 

Of course I can search in the string the info I want to extract but I would prefer to do it by parsing the whole info.

There is more than JSON there and the JSON is quite complex. Here is the JSON part.

{"statusInfo":{"status":"ONLINE","statusDetail":"NONE"},"editable":true,"label":"temperatures_heizung","bridgeUID":"km200:kmdevice:9572","configuration":{},"properties":{"root":"#system#sensors#temperatures"},"UID":"km200:sensor:9572:temperatures","thingTypeUID":"km200:sensor","channels":[{"linkedItems":["T_Aussen_Hz","km200_sensor_9572_temperatures_outdoor_t1"],"uid":"km200:sensor:9572:temperatures:outdoor_t1","id":"outdoor_t1","channelTypeUID":"km200:sensor:9572:temperatures:outdoor_t1","itemType":"Number","kind":"STATE","label":"outdoor_t1","description":"outdoor_t1","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#outdoor_t1"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_chimney"],"uid":"km200:sensor:9572:temperatures:chimney","id":"chimney","channelTypeUID":"km200:sensor:9572:temperatures:chimney","itemType":"Number","kind":"STATE","label":"chimney","description":"chimney","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#chimney"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_supply_t1_setpoint"],"uid":"km200:sensor:9572:temperatures:supply_t1_setpoint","id":"supply_t1_setpoint","channelTypeUID":"km200:sensor:9572:temperatures:supply_t1_setpoint","itemType":"Number","kind":"STATE","label":"supply_t1_setpoint","description":"supply_t1_setpoint","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#supply_t1_setpoint"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_supply_t1"],"uid":"km200:sensor:9572:temperatures:supply_t1","id":"supply_t1","channelTypeUID":"km200:sensor:9572:temperatures:supply_t1","itemType":"Number","kind":"STATE","label":"supply_t1","description":"supply_t1","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#supply_t1"},"configuration":{}},{"linkedItems":["T_Warmwasser","km200_sensor_9572_temperatures_hotWater_t1"],"uid":"km200:sensor:9572:temperatures:hotWater_t1","id":"hotWater_t1","channelTypeUID":"km200:sensor:9572:temperatures:hotWater_t1","itemType":"Number","kind":"STATE","label":"hotWater_t1","description":"hotWater_t1","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#hotWater_t1"},"configuration":{}},{"linkedItems":["km200_sensor_9572_temperatures_switch"],"uid":"km200:sensor:9572:temperatures:switch","id":"switch","channelTypeUID":"km200:sensor:9572:temperatures:switch","itemType":"Number","kind":"STATE","label":"switch","description":"switch","defaultTags":[],"properties":{"root":"#system#sensors#temperatures#switch"},"configuration":{}}]} 

You can parse it out by pasting into a site like https://jsonlint.com/

Using the cli cURL is much easier. I set my command in the variable $cmd
$curlJSON = array(json_decode(exec($cmd), TRUE));
puts the JSON into $curlJSON array to be parsed further.

Thanks a lot for the hint! I just did a search for the first { and this can then be parsed with
$json=json_decode(strstr($output,'{'),true);

Many thanks for the quick help!
Merry Xmas and a Happy New Year!
Christof