Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

jmespath

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.

like image 589
lissajous Avatar asked Jan 22 '26 19:01

lissajous


1 Answers

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]"
      }
    ]
  }
]
like image 64
β.εηοιτ.βε Avatar answered Jan 26 '26 01:01

β.εηοιτ.βε



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!