jsonjsonpathjayway

Jsonpath: get the list of elements that contain at least one id from a list of ids


With the following json:

{
  "elements": [
    {
      "ids": [
        {
          "id": "A"
        },
        {
          "id": "B"
        }
      ],
      "value": "one"
    },
    {
      "ids": [
        {
          "id": "C"
        },
        {
          "id": "D"
        }
      ],
      "value": "two"
    },
    {
      "ids": [
        {
          "id": "E",
        },
        {
          "id": "F",
        }
      ],
      "value": "three"
    }
  ]
}

What would be the jsonpath to return the list of the elements containing at least an id from the list ['A','C']?

I can get the desired result if I ask specifically for each id:

$.elements[?('A' in @.ids.*.id || 'C' in @.ids.*.id)]

[
   {
      "ids" : [
         {
            "id" : "A"
         },
         {
            "id" : "B"
         }
      ],
      "value" : "one"
   },
   {
      "ids" : [
         {
            "id" : "C"
         },
         {
            "id" : "D"
         }
      ],
      "value" : "two"
   }
]

but in my scenario I need to indicate the values of the ids within a list ['A','C']

Thanks in advance!


Solution

  • use the anyof filter operator

    $.elements[?(@.ids.*.id anyof ['A','C'])]