1

I have following JSON, I need to get the personEmailContact which has a matching emailID with emailupdaterequest.emailContact. I tried to write the expression like $.responsebody[?(@.personEmailContact.emailId ==$.emailupdaterequest.emailContact.emailId)] but this does not seem to work, I need to write an expression which can work without substitution. Can anyone help with any pointers

{
    "emailupdaterequest": {
        "emailContact": {
            "emailId": "admin@gmail.com",
            "contactName": "ADMIN EMAIL"
        }
    },
    "responsebody": [
        {
            "personEmailContact": {
                "emailId": "admin@gmail.com",
                "contactTypeCode": "P",
                "contactName": "Joe"
            }
        },
        {
            "personEmailContact": {
                "emailId": "nonadmin@gmail.com",
                "contactTypeCode": "P",
                "contactName": "john"
            }
        }
    ]
}
rsapru
  • 688
  • 14
  • 30

1 Answers1

1

You should be able to use the root selector $ inside an expression. This will allow you to compare values inside an array element to values elsewhere within the JSON:

$.responsebody[?(@.personEmailContact.emailId == $.emailupdaterequest.emailContact.emailId)]

Note the $.emailupdaterequest.emailContact.emailId.

$ indicates the path is evaluated from the root of the JSON, whereas @ indicates that the path is evaluated from the current item as the processor iterates over the array.

gregsdennis
  • 7,218
  • 3
  • 38
  • 71
  • This does not work, when i evaluated the same https://jsonpath.com/ it shows jsonPath: $ is not defined: _$_v.personEmailContact.emailId == $.emailupdaterequest.emailContact.emailId – rsapru Apr 19 '22 at 13:11
  • Well , I stand corrected, it seems https://jsonpath.com/ is not evaluating expression correctly. When i tried using com.jayway.jsonpath.JsonPath in java it worked. Thanks @gregsdennis – rsapru Apr 19 '22 at 13:21
  • 1
    Yeah, not all implementations are the same. We're working on a specification. – gregsdennis Apr 19 '22 at 19:16
  • https://jsonpath.com/ uses [jsonpath-plus](https://www.npmjs.com/package/jsonpath-plus) – Akshay G Apr 20 '22 at 05:05