ibatullin / jsonpath_goessner

Automatically exported from code.google.com/p/jsonpath
0 stars 0 forks source link

query returns to may hits despite predicate on parent element #28

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi Stefan

I have now find the correct place for Jsonpath issues!

I have posted a mini demo here:

https://github.com/sailfishapps/ThrowawayDemos/blob/master/Kaputt/JsonDBDemo

This runs on Sailfish using Qt QML / Javascript

The json file has 3 entities / elements, landed, area and template. Landed is 
the root, and template is a child of area.

The json file has 3 areas, each with 2 child templates.

If I apply the following query:

$.landed.area[?(@.id = 1)].template[*]

I expect to get only the templates that belong to the area with id 1. (i.e. 2 
hits)

Instead I get all templates of all areas. (i.e. 6 hits)

I am running jsonpath 0.8.5

As a first step to understanding what your code is doing, I have added some 
console.log calls to the trace function.

The output shows that template is being processed 3 times (once for each area). 
I expect it to be processed only once due to the predicate on area.

Grüsse

Chris

christopher.lamb@ch.ibm.com

//start json file
{
   "landed":{
      "area":[
         {
            "id":1,
            "name":"South Africa",
            "template":[
               {
                  "id":1,
                  "name":"DefaultOKMsg"
               },
               {
                  "id":2,
                  "name":"DefaultNotOKMsg"
               }
            ]
         },
         {
            "id":3,
            "name":"Schweiz",
            "template":[
               {
                  "id":1,
                  "name":"DefaultOKMsg"
               },
               {
                  "id":2,
                  "name":"DefaultNotOKMsg"
               }
            ]
         },
         {
            "id":4,
            "name":"Italia",
            "template":[
               {
                  "id":1,
                  "name":"DefaultOKMsg"
               },
               {
                  "id":2,
                  "name":"DefaultNotOKMsg"
               }
            ]
         }
      ]
   }
}
//end json file

//start console. log output
[D] getTemplates:50 - getting Templates for area: undefined
[D] parseJSONString:12 - about to pass DB to JSONPath
[D] P.trace:33 - trace: expr:landed;area;?(@.id = 1);template;*
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$
[D] P.trace:33 - trace: expr:area;?(@.id = 1);template;*
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed
[D] P.trace:33 - trace: expr:?(@.id = 1);template;*
[D] P.trace:34 - trace: val:[object Object],[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area
[D] P.trace:33 - trace: expr:0;template;*
[D] P.trace:34 - trace: val:[object Object],[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area
[D] P.trace:33 - trace: expr:template;*
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;0
[D] P.trace:33 - trace: expr:*
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;0;template
[D] P.trace:33 - trace: expr:0;
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;0;template
[D] P.trace:33 - trace: expr:
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;0;template;0
[D] P.trace:33 - trace: expr:1;
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;0;template
[D] P.trace:33 - trace: expr:
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;0;template;1
[D] P.trace:33 - trace: expr:1;template;*
[D] P.trace:34 - trace: val:[object Object],[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area
[D] P.trace:33 - trace: expr:template;*
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;1
[D] P.trace:33 - trace: expr:*
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;1;template
[D] P.trace:33 - trace: expr:0;
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;1;template
[D] P.trace:33 - trace: expr:
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;1;template;0
[D] P.trace:33 - trace: expr:1;
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;1;template
[D] P.trace:33 - trace: expr:
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;1;template;1
[D] P.trace:33 - trace: expr:2;template;*
[D] P.trace:34 - trace: val:[object Object],[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area
[D] P.trace:33 - trace: expr:template;*
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;2
[D] P.trace:33 - trace: expr:*
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;2;template
[D] P.trace:33 - trace: expr:0;
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;2;template
[D] P.trace:33 - trace: expr:
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;2;template;0
[D] P.trace:33 - trace: expr:1;
[D] P.trace:34 - trace: val:[object Object],[object Object]
[D] P.trace:35 - trace: path:$;landed;area;2;template
[D] P.trace:33 - trace: expr:
[D] P.trace:34 - trace: val:[object Object]
[D] P.trace:35 - trace: path:$;landed;area;2;template;1
[D] parseJSONString:14 - readDataModel.js: objectArray.length: 6
//end console.log output

Original issue reported on code.google.com by flyingsh...@gmail.com on 10 Feb 2014 at 9:55

GoogleCodeExporter commented 9 years ago
Hi Stefan

I have done more tests, and come to the conclusion that we have a "worst case" 
scenario: Both the syntax of my query, and jsonpath have mistakes!

I was confused by the ? sign, which means FILTER. But does it filter IN, or 
OUT? I now think it means FILTER OUT from the resultset.

This means that to see all the templates that are child os area with id 1, my 
query should be:

"$.landed.area[(@.id = 1)].template[*]"

I have run the following battery of tests using the slightly changed json file 
at the end of this comment:

The only ones that seem to return what I expect are the double negative tests 
(FILTER OUT NOT equal to)  ? !=

The positive tests return results shifted by one parent (e.g I don't get the 
childs of area 1, I get the childs of area 2).

Grüsse

Chris

    // return all templates where the area id is 0, should return nothing
    //var query = "$.landed.area[(@.id = 0)].template[*]" // returns 11 and 12 --> NOT OK

    // return all templates where the area id is 1, should return 11, 12
    //var query = "$.landed.area[(@.id = 1)].template[*]" //returns  21 and 22 --> NOT OK

    // return all templates where the area id is 2, should return 21, 22
    //var query = "$.landed.area[(@.id = 2)].template[*]" //returns 31 and 32 --> NOT OK

    // return all templates where the area id is 3, should return 31, 32
    //var query = "$.landed.area[(@.id = 3)].template[*]" //returns nothing --> NOT OK

    // Filter OUT all those where area id is 0, should return 11, 12, 21, 22, 31, 32
    //var query = "$.landed.area[?(@.id = 0)].template[*]" // returns nothing --> NOT OK

    // Filter OUT all those where area id is 1, should return 21, 22, 31, 32
    //var query = "$.landed.area[?(@.id = 1)].template[*]" // returns 11, 12, 21, 22, 31, 32 --> NOT OK

    // Filter OUT all those where area id is 2, should return 11, 12, 31, 32
    //var query = "$.landed.area[?(@.id = 2)].template[*]" // returns 11, 12, 21, 22, 31, 32 --> NOT OK

    // Filter OUT all those where area id is 3, should return 11, 12, 21, 22
    //var query = "$.landed.area[?(@.id = 3)].template[*]" // returns 11, 12, 21, 22, 31, 32 --> NOT OK

    // return all templates where the area id is NOT 0, should return 11, 12, 21, 22, 31, 32
    //var query = "$.landed.area[(@.id != 0)].template[*]" // returns nothing --> NOT OK

    // return all templates where the area id is NOT 1, should return 21, 22, 31, 32
    //var query = "$.landed.area[(@.id != 1)].template[*]" // returns nothing --> NOT OK

    // return all templates where the area id is NOT 2, should return 11, 12, 31, 32
    //var query = "$.landed.area[(@.id != 2)].template[*]" // returns nothing --> NOT OK

    // return all templates where the area id is NOT 3, should return 11, 12, 21, 22
    //var query = "$.landed.area[(@.id != 3)].template[*]" // returns nothing --> NOT OK

    // Filter OUT all those where the area id is NOT 0, should return 11, 12, 21, 22, 31, 32
    //var query = "$.landed.area[?(@.id != 0)].template[*]" // returns 11, 12, 21, 22, 31, 32 --> OK

    // Filter OUT all those where the area id is NOT 1, should return 21, 22, 31, 32
    //var query = "$.landed.area[?(@.id != 1)].template[*]" // returns 21, 22, 31, 32 --> OK

    // Filter OUT all those where the area id is NOT 2, should return 11, 22, 31, 32
    //var query = "$.landed.area[?(@.id != 2)].template[*]" // returns 11, 22, 31, 32 --> OK

    // Filter OUT all those where the area id is NOT 3, should return 11, 12, 21, 22
    //var query = "$.landed.area[?(@.id != 3)].template[*]" // returns 11, 12, 21, 22 --> OK

    //java style equals does not work either ....
    //var query = "$.landed.area[(@.id == 1 )].template[*]"

{
   "landed":{
      "area":[
         {
            "id":1,
            "name":"South Africa",
            "template":[
               {
                  "id":11,
                  "name":"DefaultOKMsg"
               },
               {
                  "id":12,
                  "name":"DefaultNotOKMsg"
               }
            ]
         },
         {
            "id":2,
            "name":"Schweiz",
            "template":[
               {
                  "id":21,
                  "name":"DefaultOKMsg"
               },
               {
                  "id":22,
                  "name":"DefaultNotOKMsg"
               }
            ]
         },
         {
            "id":3,
            "name":"Italia",
            "template":[
               {
                  "id":31,
                  "name":"DefaultOKMsg"
               },
               {
                  "id":32,
                  "name":"DefaultNotOKMsg"
               }
            ]
         }
      ]
   }
}

Original comment by flyingsh...@gmail.com on 10 Feb 2014 at 2:46

GoogleCodeExporter commented 9 years ago
SOLVED!

Some more experimentation later, it turns out that my query was (as half 
suspected) wrong!

In order to get the templates belonging to area with id 1, the query should be:

"$.landed.area[?(@.id == 1 )].template[*]"

or

"$.landed.area[?(@.id === 1 )].template[*]"

i.e contrary to my comment above ? means FILTER IN, and a java style equals is 
required.

You can close the issue, but it would be helpful (and would have saved several 
hours hacking around and hair pulling) if you could include such an example in 
your documentation. e.g. 

"$..book[?(@.price == 8.99)]" // returns Moby Dick

grüsse 

Chris

Original comment by flyingsh...@gmail.com on 10 Feb 2014 at 4:14