Forum Discussion

dd_snaplogic's avatar
dd_snaplogic
New Contributor II
3 years ago
Solved

How to check object exists in an array

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?

  • 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,

4 Replies

  • 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.

    • dd_snaplogic's avatar
      dd_snaplogic
      New Contributor II

      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

      • Abhishek_Soni37's avatar
        Abhishek_Soni37
        Contributor

        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,