โ11-12-2020 06:58 AM
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โ
}
}
},
Solved! Go to Solution.
โ11-12-2020 07:26 AM
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"
โ11-12-2020 07:26 AM
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"
โ11-12-2020 07:35 AM
Thanks for the same answer only better with Documentation links ๐
โ11-12-2020 07:27 AM
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'])