0

Consider the code snippet below-

{
    "header": {
        "systemId": "1"
    },
    "body": {
        "approvalType": "S",
        "requester": "CRM",
        "approver": "V",
        "additionalInfoList": [
            {
                "additionalInfoItem": {
                    "value": [
                        {
                            "secret": [
                                {
                                    "question": "1"
                                }
                            ]
                        },
                        {
                            "secret": [
                                {
                                    "question": "2"
                                }
                            ]
                        },
                        {
                            "secret": [
                                {
                                    "question": "3"
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "additionalInfoItem": {
                    "name": "key2",
                    "value": [
                        {
                            "secret": [
                                {
                                    "question": "00"
                                }
                            ]
                        },
                        {
                            "secret": [
                                {
                                    "question": "002"
                                }
                            ]
                        },
                        {
                            "secret": [
                                {
                                    "question": "003"
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }
}

For this json path

$.body.additionalInfoList[*].additionalInfoItem.value[*].secret[*].question 

the API gives

[
   "1",
   "2",
   "3",
   "00",
   "002",
   "003"
]

I am using configuration REQUIRE_PROPERTIES option which configures JsonPath to require properties defined in path when an indefinite path is evaluated.

If in the above JSON, one of the question is not sent in request, an exception as below will be thrown - No results for path: $['body']['additionalInfoList'][1]['additionalInfoItem']['value'][0]['secret'][0]['question']

I have a requirement to collect all other values for the question tag even when the exception com.jayway.jsonpath.PathNotFoundException was thrown. How can I achieve this?


On the other hand, if I use the option SUPPRESS_EXCEPTIONS, how can I know if there is a missing path?

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105

1 Answers1

0

I do not have a great answer here; however, to get it done I would suggest processing the response twice:

  • once with SUPPRESS_EXCEPTIONS (or simply no option might work as well),
  • and then with REQUIRE_PROPERTIES to detect the errors.

This should allow you to handle the scenario as described.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Yes. I also came across this solution of setting the properties at different points of execution. Unfortunately, the change in configuration is discouraged in a multi-threaded environment due to it being at the application-level and not at the request-level. – Farhan stands with Palestine Dec 08 '20 at 11:17