'JSONPath filter property by value in object
How can I use the JSONPath filtering capabilities to query for a specific condition on a property on a subobject (without an array)?
Consider this JSON example:
{
"queue": {
"size": 13
}
}
I want to get a match if the .queue.size
is greater than 0 and no match if it's equal to 0.
I tried using the following query but it does not work: $.queue[?(@.size>0)]
. It is unclear to me why is that since the $.queue[size]
does work returning value 13 correctly in this example, but if I include the filtering syntax I never get a match.
Solution 1:[1]
It looks like JSONPath expressions applies only to arrays. See here and here. Your query $.queue[?(@.size>0)]
works for:
{
"queue": [{
"size": 13
},
{
"size": 10
}]
}
Solution 2:[2]
a little trick could be:
$..queue[?(@.size>0)]
The only downside is that it will change every object in the subtrees if it matches the criteria. not only at the first level.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Whispored2001 |
Solution 2 | Frans |