I'm trying to parse a JSON in PHP using jsonpath ....
My JSON is coming from this
https://servizionline.sanita.fvg.it/tempiAttesaService/tempiAttesaPs
(it's quite too long to cut/paste here but you can see it in a browser session ....)
The JSON is a valid JSON (I've verified it using https://jsonlint.com/ ... ).
I've tried the jsonpath expression using http://www.jsonquerytool.com/ and all seems works fine, but when I put all in my PHP code sample below ....
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('json.php'); // JSON parser
require_once('jsonpath-0.8.0.php'); // JSONPath evaluator
$url = 'https://servizionline.sanita.fvg.it/tempiAttesaService/tempiAttesaPs';
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PROXY, '');
$data = curl_exec($ch);
curl_close($ch);
$parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$o = $parser->decode($data);
$xpath_for_parsing = '$..aziende[?(@.descrizione=="A.S.U.I. - Trieste")]..prontoSoccorsi[?(@.descrizione=="Pronto Soccorso e Terapia Urgenza Trieste")]..dipartimenti[?(@.descrizione=="Pronto Soccorso Maggiore")]..codiciColore[?(@.descrizione=="Bianco")]..situazionePazienti..numeroPazientiInAttesa';
$match1 = jsonPath($o, $xpath_for_parsing);
//print_r($match1);
$match1_encoded = $parser->encode($match1);
print_r($match1_encoded);
$match1_decoded = json_decode($match1_encoded);
//print_r($match1_decoded);
if ($match1_decoded[0] != '') {
return $match1_decoded[0];
}
else {
return "N.D.";
}
?>
... no values are printed .. only a "false" value.
Something goes wrong in my jsonpath expression when I put it in my PHP code: ths error that coming out is the follow
Warning: Missing argument 3 for JsonPath::evalx(), called in /var/www/html/OpenProntoSoccorso/Test/jsonpath-0.8.0.php on line 84 and defined in /var/www/html/OpenProntoSoccorso/Test/jsonpath-0.8.0.php on line 101
Notice: Use of undefined constant descrizione - assumed 'descrizione' in /var/www/html/OpenProntoSoccorso/Test/jsonpath-0.8.0.php(104) : eval()'d code on line 1
Probably I've to escape / quoting my jsonpath to use it in PHP but I don't know how ... any suggestion is appreciated ...
NOTE: I need to use jsonpath expressions like ?(@.descrizione=="A.S.U.I. - Trieste")
and I can't use "positional" json path ...
I've also tried to use jsonpath-0.8.3.php coming from here https://github.com/ITS-UofIowa/jsonpath/blob/master/jsonpath.php, but nothing change ...
Suggestions?
Thank you in advance ...
JSONPath creates a uniform standard and syntax to define different parts of a JSON document. JSONPath defines expressions to traverse through a JSON document to reach to a subset of the JSON. This topic is best understood by seeing it in action. We have created a web page which can help you evaluate a JSONPath.
JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.
I would suggest trying to use a different library for JsonPath, if the one you are working with has a bug and the 3rd party service states no error in query.
Here are a few:
I am pretty sure there are more. Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With