jsonpath save if condition exists

Hello Gatling Users,

Must have asked this question before but not able to find an answer.

Question -

In my response I get following JSON document.

{
“total_count”: 1,
“messages”: [
{
“count”: 50,
“status_code”: “PASSED”,
“type”: “INFO”
},
{
“count”: 20,
“status_code”: “FAILED”,
“type”: “INFO”
}
],
}

I do this in my Scenario, to get FAILED count from json document -

.check(jsonPath("$.messages[?(@.status_code == ‘FAILED’)].count").saveAs(“failed_messages”))

But sometimes I will not have FAILED node in my response. So the condition “$.messages[?(@.status_code == ‘FAILED’)].count” is reported as failed in Gatling output.

How Can I check “$.messages[?(@.status_code == ‘FAILED’)]” exist before taking “count” and saving it using saveAs()?

Thanks in advance!

Jay

I found it, I can add ‘optional’ before saveAs() to avoid the failure in output.

.check(jsonPath("$.messages[?(@.status_code == ‘FAILED’)].count").optional.saveAs(“failed_messages”))

Thanks!
Jay