cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Divide a comma separated list of strings

Max
New Contributor II

Hello,

I have a requirement to convert the following MuleSoft dataweave code into snap logic...

rpns default '' splitBy ',' distinctBy $ divideBy 245 map ((item, index) -> 'searchCriteria': (item joinBy ','))

I will receive a json payload like below. I could receive hundreds of rpns and I need to divide the rpns list by 245

Example:

{
"searchCriteria":[
{
"keyName": "rpns",
"values": "DUCT123-P,DUCT234-FM,DUCT345-MM,DUCT456-P,DUCT567-MM,
DUCT678-A,DUCT789-FM,DUCT891-MM,DUCT901-P,DUCT012-MM"
}
]
}
 
In this example, I need to divide the rpns string above by 2,  so I get 5 objects of 2  strings values each.  Below is an example of expected output. Does anyone know how this can be accomplished? Thank you for your help.
 
Expected output:
[
{
"searchCriteria":
"DUCT123-P,DUCT234-FM"
},
{
"searchCriteria":
"DUCT345-MM,DUCT456-P"
},
{
"searchCriteria":
"DUCT567-MM,DUCT678-A"
},
{
"searchCriteria":
"DUCT789-FM,DUCT891-MM"
},
{
"searchCriteria":
"DUCT901-P,DUCT012-MM"
}
]
 
1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@Max - please download the attached ZIP file, decompress it, and import the SLP as a POC pipeline.  

koryknick_0-1706729956579.png

There are many other ways to solve this and others in the Community may provide you with alternate solutions, but I wanted to try to keep this example fairly simple.

"Split values" uses a JSON Splitter snap and jsonPath function to get the searchCriteria.values array as individual documents.  This is done in the event you have more than one set of searchCriteria array elements.  

"Split comma separated string" uses a Mapper snap with the String.split() method to change the comma-separated list into an array for each element.  

"Split on array" uses another JSON Splitter to turn each array element into a JSON document.

"Group by 2" is a Group by N snap that simply combines the incoming documents into arrays of size N: 2 in this case.

Finally, "Create comma separated list" is another Mapper that uses jsonPath and the Array.join() method to recreate a single string per input document, which is a comma-separated list for your search criteria in the requested format.

Hope this helps!

View solution in original post

1 REPLY 1

koryknick
Employee
Employee

@Max - please download the attached ZIP file, decompress it, and import the SLP as a POC pipeline.  

koryknick_0-1706729956579.png

There are many other ways to solve this and others in the Community may provide you with alternate solutions, but I wanted to try to keep this example fairly simple.

"Split values" uses a JSON Splitter snap and jsonPath function to get the searchCriteria.values array as individual documents.  This is done in the event you have more than one set of searchCriteria array elements.  

"Split comma separated string" uses a Mapper snap with the String.split() method to change the comma-separated list into an array for each element.  

"Split on array" uses another JSON Splitter to turn each array element into a JSON document.

"Group by 2" is a Group by N snap that simply combines the incoming documents into arrays of size N: 2 in this case.

Finally, "Create comma separated list" is another Mapper that uses jsonPath and the Array.join() method to recreate a single string per input document, which is a comma-separated list for your search criteria in the requested format.

Hope this helps!