piro91
piro91

Reputation: 95

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.

Upvotes: 2

Views: 4699

Answers (2)

Frans
Frans

Reputation: 131

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.

Upvotes: 0

Whispored2001
Whispored2001

Reputation: 411

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
  }]
}

Upvotes: 2

Related Questions