Galbar/JsonPath-PHP

How can I `and` connect two filter expressions

Closed this issue · 3 comments

bwl21 commented
$.grouptypeMemberstatus[?(@.bezeichnung == 'Teenhelfer' && @.gruppentyp_id == '1')]

works in https://extendsclass.com/jsonpath-tester.html, but in JsonPath-PHP it does not find a match.

I also tried to useand instead of && (as indicated in readme) but this does not work either.

Any help welcome

Hi @bwl21,

I just tried with the this query $.store.book[?(@.category == "fiction" and @.author == "Herman Melville")] with the example json in he README.md and got [{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99,"available":true}] as a result.

and should work just fine. Could you share a minimal example to reproduce your issue?

bwl21 commented

Thanks for your reply.

<?php

require 'vendor/autoload.php';

use Flow\JSONPath\JSONPath;


$a = <<< EOT

{
  "grouptypeMemberstatus": {
    "8": {
      "id": "8",
      "gruppentyp_id": "1",
      "bezeichnung": "Teilnehmer"
    },
    "9": {
      "id": "9",
      "gruppentyp_id": "3",
      "bezeichnung": "Teilnehmer"
    }
  }
}

EOT;

$b = json_decode($a);

$c = new JSONPath($b);

$path = "$.grouptypeMemberstatus[?(@.bezeichnung == 'Teilnehmer' and @.gruppentyp_id == '1')].id";
$d = $c->find($path)[0];
var_dump("$path: $d");

$path = "$.grouptypeMemberstatus[?(@.gruppentyp_id == '1')].id";
$e = $c->find($path)[0];
var_dump("$path: $e");

$path = "$.grouptypeMemberstatus[?(@.bezeichnung == 'Teilnehmer')].id";
$f = $c->find($path)[0];
var_dump("$path: $f");

php testjsopath.php yields

PHP Notice:  Undefined offset: 0 in ctapishowcase/vendor/softcreatr/jsonpath/src/AccessHelper.php on line 101

Notice: Undefined offset: 0 in ctapishowcase/vendor/softcreatr/jsonpath/src/AccessHelper.php on line 101

string(88) "$.grouptypeMemberstatus[?(@.bezeichnung == 'Teilnehmer' && @.gruppentyp_id == '1')].id: "
string(56) "$.grouptypeMemberstatus[?(@.gruppentyp_id == '1')].id: 8"
string(63) "$.grouptypeMemberstatus[?(@.bezeichnung == 'Teilnehmer')].id: 8"

you see that the first entry with the and does not yield a result.

BTW. It would be great if and were &&

bwl21 commented

Sorry, I just discovered that I was using https://github.com/SoftCreatR/JSONPath.

I'll try my testcase with your implementation now.
with your implementation it works as expected.