Search code examples
jsonjmespath

JMESPpath: filtering out by nested attributes


I am trying to apply the filter using JMESPath jp (https://github.com/jmespath/jp) utility. My goal is to have only the flow whose state is 'ADDED' and having specific device id (e.g. 0000debf17cff54b) filtered out.

I am trying something like this: cat test | ./jp '[][?id=="of:00002259146f7743" && state=="ADDED"]' but the result is []

[
  {
    "flow": [
      {
        "ethType": "0x86dd",
        "type": "ETH_TYPE"
      },
      {
        "protocol": 58,
        "type": "IP_PROTO"
      },
      {
        "icmpv6Type": 135,
        "type": "ICMPV6_TYPE"
      }
    ],
    "id": "of:00001aced404664b",
    "state": "ADDED"
  },
  {
    "flow": [
      {
        "ethType": "0x86dd",
        "type": "ETH_TYPE"
      },
      {
        "protocol": 58,
        "type": "IP_PROTO"
      },
      {
        "icmpv6Type": 136,
        "type": "ICMPV6_TYPE"
      }
    ],
    "id": "of:0000debf17cff54b",
    "state": "ADDED"
  }
]

Solution

  • No need to use the first [], [?id=='of:0000debf17cff54b' && state=='ADDED'] works fine.

    Using the first [] gives you the entire array, that does not contain a id or state keys.