2

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?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
James World
  • 29,019
  • 9
  • 86
  • 120

3 Answers3

3

You can use

[?length(b)>'0']
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
1

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

[?not_null(b)]
James World
  • 29,019
  • 9
  • 86
  • 120
0

Extendind @Dominik Brázdil answer, was able to achieve following in aws cli command.

Note : Double quotes " did not work, use single quotes '

aws apigateway get-usage-plans --query 'items[?length(apiStages[])>`0`].id' --max-items 5 --profile TPaaSProd

If you want to query json items whose specific particular value is empty or length is zero (0).

aws apigateway get-usage-plans --query 'items[?length(apiStages[])==`0`].id' --max-items 5 --profile TPaaSProd
SRi
  • 1,046
  • 7
  • 10