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

How to check object exists in an array

dd_snaplogic
New Contributor II

I have an array eg
{
โ€œgroupByโ€: {
โ€œrowKeyโ€: โ€œ0141116963814โ€
},
โ€œgroupโ€: [
{
โ€œrowKeyโ€: โ€œ0141116963814โ€
},
{
โ€œrowKeyโ€: โ€œ0141116963814โ€
},
{
โ€œrowKeyโ€: โ€œ0141116963814โ€
}
]
}

Now Iโ€™m accessing each element of group in mapper and I require to have the null-safe access check enabled.

Now if Iโ€™m trying to check whether the group array have object in 7th position ie jsonPath($, โ€œ$group[6].rowKey[0]โ€) but, iโ€™m getting null as a response

Iโ€™ve tried using various ways (eg. sl.ensureArray(jsonPath($, โ€œ$group[6]โ€))) but it seems when null-safe access check is enabled nothing works
What is the way to check whether the element or object exists?

1 ACCEPTED SOLUTION

You need to put $group[6], inside quotes like $.hasPath(โ€œ$group[6]โ€), if there is no item at index 6 in the group array, this expression will return false.

Cheers,

View solution in original post

4 REPLIES 4

Abhishek_Soni37
Contributor

You can use the below expression to see if the element is an object or not.

typeof(input)

On the other hand, if you want to check if a specific element/path exist or not, use below. Docs

$.hasPath(โ€œ$putThePathโ€)

Let us know if it helps.

No when an element doesnโ€™t exists and the null-safe option is enabled it is giving me null as a value no matter what condition Iโ€™m passing.

ie typeof($group[6]) will return null as well $.hasPath($group[6]) will return null

You need to put $group[6], inside quotes like $.hasPath(โ€œ$group[6]โ€), if there is no item at index 6 in the group array, this expression will return false.

Cheers,

ok, thanks @Soni37.