Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JMESPath, filter array elements based on length of an array-valued property

Tags:

jmespath

Given an input like this:

[
  {
    "a": "foo",
    "b": [ 1, 2, 3 ]
  },
  {
    "a": "bar",
    "b": [ ]
  },
  {
    "a": "baz",
    "b": [ 2 ]
  }
]

I want to filter out the elements that have a zero-length array for their b property to give:

[
  {
    "a": "foo",
    "b": [ 1, 2, 3 ]
  },
  {
    "a": "baz",
    "b": [ 2 ]
  }
]

How do I do this with JMESPath?

like image 992
James World Avatar asked Mar 18 '26 22:03

James World


1 Answers

You can use

[?length(b)>'0']
like image 119
Dominik Brázdil Avatar answered Mar 24 '26 09:03

Dominik Brázdil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!