lissajous
lissajous

Reputation: 451

How to use JMESPath to filter array of object based on contains of value in subproperty arrary

How use JMESPath to filter nodes that possess email receivers with exact email? I have JSON object:

   [     
     {
        "test":1,
        "emailReceivers": [
          {
            "emailAddress": "[email protected]",
          }
        ]
      },
      {
        "test":2,
        "emailReceivers": [
          {
            "emailAddress": "[email protected]",
          },
          {
            "emailAddress": "[email protected]",
          }
        ]
      }
    ]

I would like to get node after filtering by elememailReceivers that contains [email protected]:

     {
        "test":1,
        "emailReceivers": [
          {
            "emailAddress": "[email protected]",
          }
        ]
      }

I was trying to use documentation https://jmespath.org/ and examples but I failed.

Upvotes: 3

Views: 7616

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39079

You can indeed use contains on a filter and ask sub-keys containing a certain value.

With the query

[?contains(emailReceivers[].emailAddress, '[email protected]')]

This will give:

[
  {
    "test": 1,
    "emailReceivers": [
      {
        "emailAddress": "[email protected]"
      }
    ]
  }
]

On your example array:

[     
  {
    "test":1,
    "emailReceivers": [
      {
        "emailAddress": "[email protected]"
      }
    ]
  },
  {
    "test":2,
    "emailReceivers": [
      {
        "emailAddress": "[email protected]"
      },
      {
        "emailAddress": "[email protected]"
      }
    ]
  }
]

Upvotes: 4

Related Questions