How to convert String to an Array in OIC?
Method 1:
Tokenize Function.
The tokenize function is the standard XSLT method to split a string into a sequence based on a delimiter (like a comma, pipe, or space).
It Returns a sequence of strings constructed by splitting the input wherever a separator is found; the separator is any substring that matches a given regular expression.
General Syntax in OIC:
tokenize($arg1 as string?, $arg2 as string?, $flag as string?)
tokenize(input_string1, input_string2,.......delimiter)
Parameters
$arg1: The string to tokenize.
$arg2: The regular expression to be used as a separator.
$flag: An optional string containing character flags.
Example:
tokenize(input_string, delimiter)
tokenize("Welcome to OIC", ",")
Results in a list: ["Welcome", "to", "OIC"]
Note:
When using tokenize, if your string might have spaces after the comma
(Ex,"Apple, Orange"), use normalize-space() or a regex delimiter like tokenize($SourceField, ',\s*') to ensure your array elements are clean.
Steps to use Tokenize function
Open the Data Mapper in your integration.
Locate the target Repeating Element (the array/collection) where you want the data to go.
Drag the source string field to the target repeating element.
In the expression builder, wrap the source field with the tokenize function:
tokenize($SourceField, ',')
OIC will automatically treat the output of tokenize as a sequence, creating one target record for every item in your string.
Method 2:
Handling Complex Strings (Using Stage File)
If your string is very large or formatted like a CSV file, using the Stage File action is more robust.
Action: Choose Stage File and select Read File in Segments or Map to Report Data.
Schema: Define a simple CSV opaque schema (Ex: a single column value).
Process: We can write the string to a temporary file using Write File and then read it back. This is overkill for simple lists but helpful for high-volume data processing.
Method 3:
JavaScript Action (For Custom Logic)
If we need to do more than just split (Ex:- trimming whitespace or filtering empty values during the split), a Library Function (JavaScript) is best.
function stringToArray(inputString, delimiter) {
if (!inputString) return [];
return inputString.split(delimiter).map(item => item.trim());
}
How to use JavaScript functions in OIC?
Create a Library in OIC and upload this JS code.
In your integration, call the Library Function.
Map the output of this function to your target array.
No comments:
Post a Comment