'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 1:[1]

You can use

[?length(b)>'0']

Solution 2:[2]

I figured this out, simple in the end although the function name is a little misleading. All you need is this:

[?not_null(b)]

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 β.εηοιτ.βε
Solution 2 James World