1

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.

piro91
  • 85
  • 1
  • 8

2 Answers2

2

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
  }]
}
Whispored2001
  • 411
  • 1
  • 5
  • Thanks for the links. It looks like this is indeed not possible with current JSONPath but people are interested in adding it. – piro91 Nov 05 '21 at 11:07
0

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.

Frans
  • 131
  • 1
  • 10