JsonPath filtering

Hi,

I am trying to use filtering expression without success.
Regarding this online tester : http://www.jsonquerytool.com/ It should work, but it’s not the case. Either Gatling found nothing or it throws an extract error.

I tried som filtered expressions which works with the online tool but not with gatling :

$…changes…[?(@.selectmode)]…id => “1012”

$…[?(@.selectmode)]…id => “1012”

$…[?(@.selectmode)].id => “1012” (message de gatling: could not extract : string matching regex [$_\p{L}][$_\-\d\p{L}]*' expected but [’ found)

$…*[?(@.selectmode)].id => “1012” (message de gatling: found nothing)

$…*[?(@.selectmode)]…id => “1012”

$.[?(@.selectmode)]…id => “1012”

$…changes[?(@[2][1].selectmode)][2][1].id => “1012”

To avoid extract errors, I must put a star in front of the filter expression.

To go further, from the goessner spec, @ match the current object. So I think I can use it to select an item from an array like this :
$…*[?(@[0]==‘rows’)]…key => [191, 192, 193, …, 209]

It does works with the online tool, but not with gatling.

Does somebody can help me to deal with gatling jsonpath filters ?

Thanks

My JSON :
[{“changes”:[[“change”,{“pid”:“520”},[“0”,{“id”:“520”,“location”:“foo”,“v”:{“action”:""}},[“actions”,{},[“action”,{“key”:“1”,“kc”:81,“mk”:[“18”,“16”,“17”]}],[“action”,{“key”:“2”,“kc”:13,“mk”:[]}],[“action”,{“key”:“3”,“kc”:13,“mk”:[]}]]]],[“change”,{“pid”:“1012”},[“23”,{“id”:“1012”,“multiselectmode”:1,“selectmode”:“multi”,“cols”:13,“rows”:19,“firstrow”:0,“totalrows”:20,“pagelength”:19,“colheaders”:true,“colfooters”:false,“vcolorder”:[“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“11”,“12”,“13”],“pb-ft”:0,“pb-l”:18,“clearKeyMap”:true,“v”:{“selected”:[],“firstvisible”:0,“sortcolumn”:“null”,“sortascending”:true,“reqrows”:-1,“reqfirstrow”:-1,“columnorder”:[“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“11”,“12”,“13”],“collapsedcolumns”:[],“noncollapsiblecolumns”:[“1”]}},[“rows”,{},[“tr”,{“key”:191,“style-4”:“perfectMatch”},""],[“tr”,{“key”:192,“style-5”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:193,“style-4”:“perfectMatch”,“style-5”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:194,“style-4”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:195,“style-4”:“perfectMatch”},""],[“tr”,{“key”:196},""],[“tr”,{“key”:197},""],[“tr”,{“key”:198},""],[“tr”,{“key”:199},""],[“tr”,{“key”:200},""],[“tr”,{“key”:201},""],[“tr”,{“key”:202},""],[“tr”,{“key”:203},""],[“tr”,{“key”:204,“style-4”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:205,“style-4”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:206,“style-4”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:207,“style-4”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:208,“style-4”:“perfectMatch”,“style-10”:“perfectMatch”},""],[“tr”,{“key”:209},""]],[“visiblecolumns”,{},[“column”,{“cid”:“1”,“caption”:“foo”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“2”,“caption”:“bar”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“3”,“caption”:“baz”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“4”,“caption”:“too”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“5”,“caption”:“xxx”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“6”,“caption”:“yyy”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“7”,“caption”:“zzz”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“8”,“caption”:“aaa”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“9”,“caption”:“bbb”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“10”,“caption”:“ccc”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“11”,“caption”:“ddd2”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“12”,“caption”:“eee”,“fcaption”:"",“sortable”:true}],[“column”,{“cid”:“13”,“caption”:“fff”,“fcaption”:"",“sortable”:true}]]]],[“change”,{“pid”:“997”},[“1”,{“id”:“997”}]],[“change”,{“pid”:“1011”},[“1”,{“id”:“1011”}]],[“change”,{“pid”:“996”},[“1”,{“id”:“996”}]],[“change”,{“pid”:“994”},[“1”,{“id”:“994”}]],[“change”,{“pid”:“999”},[“1”,{“id”:“999”}]],[“change”,{“pid”:“993”},[“1”,{“id”:“993”}]],[“change”,{“pid”:“995”},[“1”,{“id”:“995”}]],[“change”,{“pid”:“1003”},[“1”,{“id”:“1003”}]],[“change”,{“pid”:“990”},[“1”,{“id”:“990”}]],[“change”,{“pid”:“992”},[“1”,{“id”:“992”}]],[“change”,{“pid”:“1005”},[“1”,{“id”:“1005”}]],[“change”,{“pid”:“1001”},[“1”,{“id”:“1001”}]],[“change”,{“pid”:“991”},[“1”,{“id”:“991”}]]],“state”:{“520”:{“pollInterval”:-1},“1011”:{“text”:"(20)"}},“types”:{“520”:“0”,“990”:“1”,“991”:“1”,“992”:“1”,“993”:“1”,“994”:“1”,“995”:“1”,“996”:“1”,“997”:“1”,“999”:“1”,“1001”:“1”,“1003”:“1”,“1005”:“1”,“1011”:“1”,“1012”:“23”},“hierarchy”:{“520”:[“983”,“521”,“984”],“990”:[],“991”:[],“992”:[],“993”:[],“994”:[],“995”:[],“996”:[],“997”:[],“999”:[],“1001”:[],“1003”:[],“1005”:[],“1011”:[],“1012”:[]},“rpc”:[],“meta”:{},“resources”:{},“timings”:[4509,1]}]

https://github.com/gatling/jsonpath/blob/master/src/main/scala/io/gatling/jsonpath/Parser.scala#L166 :slight_smile:

2 things:

  • The “spec” from Goessner is sadly incomplete and sometimes vague. It would be great to have a real spec driven by the W3C, as there’s a lot of interest and implementations in the wild for such “XPath for JSON”. Keep in mind that it’s just a short blog post from 2007.

  • Even Goessner’s own implementation is completely broken and can’t be trusted, see below
    Regarding, your samples, I agree they should all work (fix is on the way), BUT the 6th one.

$.[?(@.selectmode)]…id shouldn’t return “1012” as root doesn’t have a direct child that has a selectmode field.

Hi,

I appreciate your Job Stéphane, keep going.

I just tested the master v0.6.5-SNAPSHOT this morning.
Firstly, I had to build it with scalaVersion := “2.11.7” in build.sbt (I use gatling 2.2.0-M3)

I confirm jsonPath("$…changes…*[?(@.key)].key").findAll and jsonPath("$…changes…[?(@.key)].key").findAll now works and retrieve an array of keys.

But i tried this : jsonPath("$…changes…*[?(@.selectmode)]…id").find(1) and the parser found nothing. I have to use findAll to find the unique result. Is this behaviour expected ?

Moreover, I have a question about ofType() : I use it to filter the results. Currently, jsonPath("$…changes…*[?(@.key)].key").ofType[Int] don’t return key values of type String, only values of type Int. Is this a feature I can rely on ?

Ok, my mistake, find(1) return the second result not the first one.

Moreover, I have a question about ofType() : I use it to filter the results.

ofType applies downstream of the path, so you can filter with it, but it will only apply to the JsonPath output.