cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Json Path, when some objects don't contain the path

Tommy
New Contributor III

Hello, i have a pipeline that is extracting values from an array using JSON path. This is how it worksโ€ฆ

Extract the status and effective data from the array where the โ€œactivityโ€ = x

image

I use this expressionโ€ฆ

jsonPath($, โ€œ$regulatedActivities[?(@.Activity==โ€˜Credit Broking (W.E.F 01/04/2014)โ€™)].Statusโ€)[0]

All works fine. The problem is, in some cases some items in the array do not contain โ€œActivityโ€ at all. There are subsequent items in the array containing this element, but the expression doesnโ€™t work in this example, it just returns NULLโ€ฆ

image

image

1 ACCEPTED SOLUTION

bojanvelevski
Valued Contributor

You can switch to more detailed and easier to control expression. Something like:

jsonPath($, "$regulatedActivities[*]").filter(x=>x.Activity == 'Credit Broking (W.E.F 01/04/2014)')

The result from this will be an array filtered by Activity. If you want only the first object ( [0] ) and you want to take the Status from it, than use the following expression:

jsonPath($, "$regulatedActivities[*]").filter(x=>x.Activity == 'Credit Broking (W.E.F 01/04/2014)')[0].Status

Let me know if this helps,
Bojan

View solution in original post

2 REPLIES 2

bojanvelevski
Valued Contributor

You can switch to more detailed and easier to control expression. Something like:

jsonPath($, "$regulatedActivities[*]").filter(x=>x.Activity == 'Credit Broking (W.E.F 01/04/2014)')

The result from this will be an array filtered by Activity. If you want only the first object ( [0] ) and you want to take the Status from it, than use the following expression:

jsonPath($, "$regulatedActivities[*]").filter(x=>x.Activity == 'Credit Broking (W.E.F 01/04/2014)')[0].Status

Let me know if this helps,
Bojan

Tommy
New Contributor III

Amazing. Thank you