Search code examples
jmespath

how to select only objects that contain an array with more than N elements


What's the right JMESPath expression to capture only the second object in the following array (because it has more than 2 objects in its topics array):

[{
  "topics": [
    "just one"
  ]
 },
 {
  "topics": [
    "first",
    "second",
    "third"
  ]
 }
]

I want it to spit out

{
  "topics": [
    "first",
    "second",
    "third"
  ]
}

I've tried [? length(topics) > 2] but jp complains about:

SyntaxError: Invalid token: tNumber
[? length(topics) > 2]
                    ^

Solution

  • For an input:

    [{
      "topics": [
        "just one"
      ]
     },
     {
      "topics": [
        "first",
        "second",
        "third"
      ]
     }
    ]
    

    use jmesExpression:

    [?length(topics)>'2']
    

    to get output:

    [
      {
        "topics": [
          "first",
          "second",
          "third"
        ]
      }
    ]