cancel
Showing results for 
Search instead for 
Did you mean: 

Expression using isArray?

acesario
Contributor II

Does snaplogic support the use if isArray (or similar function)?

We are hitting multiple issues across pipelines where the inbound data is sometimes simply a value and sometimes an array. Would like to be able to check this, and create the array if not already an array.

For example in the below response data, wd:Section_Listing_Data an sometimes array, other times a single section of data. How to determine “isArray” and create an array if wd:Section_Listing_Data is not an array?

Blockquote
JSON with array, based on multiple ID’s

                "wd:Section_Listing_Data": [
                    {
                        "wd:ID": "COURSE_SECTION-6-25038",
                        "wd:Course_Listing_Reference": {
                            "wd:ID": {
                                "@wd:type": "Course_Listing_ID",
                                "$": "COURSE_LISTING-6-12131"
                            }
                        },
                        "wd:Section_Number": "01",
                        "wd:Course_Listing_Number": "999",
                        "wd:Course_Listing_Subject_Reference": {
                            "wd:ID": {
                                "@wd:type": "Course_Subject_ID",
                                "$": "MTH"
                            }
                        }
                    },
                    {
                        "wd:ID": "COURSE_SECTION-6-25039",
                        "wd:Course_Listing_Reference": {
                            "wd:ID": {
                                "@wd:type": "Course_Listing_ID",
                                "$": "COURSE_LISTING-6-12132"
                            }
                        },
                        "wd:Section_Number": "01",
                        "wd:Course_Listing_Number": "999",
                        "wd:Course_Listing_Subject_Reference": {
                            "wd:ID": {
                                "@wd:type": "Course_Subject_ID",
                                "$": "SDS"
                            }
                        }
                    }
                ],

JSON with obj, due to single ID

“wd:Section_Listing_Data”: {
“wd:ID”: “COURSE_SECTION_LISTING_SOCW780-1_AP_202130”,
“wd:Course_Listing_Reference”: {
“wd:ID”: {
@wd:type”: “Course_Listing_ID”,
“$”: “COURSE_LISTING_SOCW780”
}
},
“wd:Section_Number”: “1”,
“wd:Course_Listing_Number”: “780”,
“wd:Course_Listing_Subject_Reference”: {
“wd:ID”: {
@wd:type”: “Course_Subject_ID”,
“$”: “SOCW”
}
}
},

1 ACCEPTED SOLUTION

del
Contributor III

There are a couple of options, maybe more.

I use sl.ensureArray quite frequently. It converts the object to an array, if it is not already an array. sl.ensureArray($myObjectOrArray)

Also available is typeof, which comes in handy in other cases. typeof $myObjectOrArray == "array"

View solution in original post

3 REPLIES 3

del
Contributor III

There are a couple of options, maybe more.

I use sl.ensureArray quite frequently. It converts the object to an array, if it is not already an array. sl.ensureArray($myObjectOrArray)

Also available is typeof, which comes in handy in other cases. typeof $myObjectOrArray == "array"

Thanks for the same answer only better with Documentation links 🙂

cjhoward18
Employee
Employee

@acesario

you can use this expression below mapped to the target $wd:Section_Listing_Data, which uses the typeof functionality:

typeof $['wd:Section_Listing_Data'] == "array" ? $['wd:Section_Listing_Data'] : [$['wd:Section_Listing_Data']]

If the type is an array, it will just pass the value along otherwise it will create an array containing the value.

Alternatively, you can use the ensureArray() function with this expression as well:

sl.ensureArray($['wd:Section_Listing_Data'])