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'])