简体   繁体   中英

Jayway JsonPath filtering with predicates

I've recently taken up Jayway JsonPath and I've had trouble with how the inpath filtering works.

So my JSON looks like this:
At the top I have shareables. These shareables have an array called user, which contains an ID and a name, and they also contain an item called dataset, which can contain any json.
These shareables can exist within the dataset as well.
My working JSON looks like this:

 {
    "shareable": {
        "user": [ 
            {
                "ID": 1,
                "Name": "Bob"
            },
            {
                "ID": 2,
                "Name": "Charles"
            }
        ],
        "dataSet": [
            { 
                "insulinMeasurement": 
                {
                    "timestamp": "Tuesday Morning",
                    "measurement": 174,
                    "unit": "pmol/L"
                } 
            },
            { 
                "insulinMeasurement": 
                {
                    "timestamp": "Tuesday Noon",
                    "measurement": 80,
                    "unit": "pmol/L"
                } 
            },
            { "shareable": {
                "user": [
                    { 
                        "ID": 3,
                        "Name": "Jim" 
                    }
                ],
                "dataSet": [
                    { 
                        "insulinMeasurement": 
                        {
                            "timestamp": "Tuesday Evening",
                            "measurement": 130,
                            "unit": "pmol/L"
                        } 
                    }
                ]
                }
            },
            { "unshareable": {
                "user": [ 
                    { 
                            "ID": 2,
                            "Name": "Bob"
                    }
                ],
                "dataSet": [
                    { 
                        "insulinMeasurement": 
                        {
                            "timestamp": "Tuesday Night",
                            "measurement": 130,
                            "unit": "pmol/L"
                        } 
                    }
                ]
                }
            }
        ]
    }
}  

So what I want is, all shareables that have a user with a certain ID. So I figured the path I would use would look like this:

$..shareable[ ?(@.user[*].ID == 1 )]

which here has a hardcoded ID. This returns nothing while

$..shareable[ ?(@.user[0].ID == 1 )]

returns any shareable where the first ID is 1.
I also tried something along the lines of

$..shareable[ ?(@.user[?(@.ID == 1)]

which I figure should return any shareable that has a user with an ID of 1.
Am I going about this the wrong way? Do I need to somehow iterate through the user objects that exist?

Well, I figured it out, so if anyone stumbles across this, the query should look as follows:

$..shareable[?( " + user + " in @.user[*].ID )]

where user is just the int of the userId. Basically the right hand side creates a list of all IDs that shareable contains, and checks if the requested ID exists therein.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM