Extract parent value based on Child value filters from JSON response

Hi Stephane,

I am having an scenario wherein I need to extract a parent value from Json response based on the filter applied on the child value.

Below is the sample response I need to filter the employeeId value based on the filter applied on the child value of severity == NO_SAVE.

$…ruleViolations[?(@.ruleViolationItems.severity=“NO_SAVE”)].employeeId

I have tried the above one but it provides all the employeeId. Could you please help on this.

{
“ruleViolations”: [
{
“ruleType”: “EMPLOYEE”,
“employeeId”: “158916ASFDF”,
“locationPosition”: null,
“ruleViolationItems”: [
{
“date”: “2020-10-02”,
“severity”: “NO_SAVE”,
“message”: “A”
}
]
},
{
“ruleType”: “EMPLOYEE”,
“employeeId”: “1334afdfd”,
“locationPosition”: null,
“ruleViolationItems”: [
{
“date”: “2020-09-28”,
“severity”: “WARNING”,
“message”: “A”
},
{
“date”: “2020-10-02”,
“severity”: “WARNING”,
“message”: “B”
}
]
},
{
“ruleType”: “EMPLOYEE”,
“employeeId”: “1334afdfd”,
“locationPosition”: null,
“ruleViolationItems”: [
{
“date”: “2020-10-02”,
“severity”: “NO_SAVE”,
“message”: “A”
}
]
}
],
“shifts”: []
}

I managed to get it work with :
$…ruleViolations[?(@.ruleViolationItems[?(@.severity==“NO_SAVE”)])].employeeId

Inspiration from StackOverflow : https://stackoverflow.com/questions/46419321/json-path-filter-on-child-of-an-array-within-another-array

Tested only with https://jsonpath.herokuapp.com/ (“Gatling” tab, obviously)

Hi Sebastien,

Thanks a lot for your solution. It works flawlessly. I tried with few nested filters as well but I tried in https://jsonpath.com/ evaluator and it didn’t work. https://jsonpath.herokuapp.com/ site doesn’t load sometimes thats the reason I moved to the other one for evaluation sometime back, since it has a Gatling tab now I will use this going forward.

Have a happy weekend :)!

Regards,
Aravind

Always happy to help!

I tried https://jsonpath.com/ first too.
But as the SO link stated, this works only with Gatling jsonpath parser.
I found the https://jsonpath.herokuapp.com/ by chance after that!