Search code examples
jmespath

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


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?


Solution

  • You can use

    [?length(b)>'0']