cancel
Showing results for 
Search instead for 
Did you mean: 

Merge multiple documents to an array in ultra pipeline

thilkip
New Contributor

We need to merge multiple documents to single array and looking for help. Since it is ultra task, we are running out of options…

Input data:

[
{“Record”: “1”},
{“Record”: “2”},
{“Record”: “5”},
{“Record”: “4”},
{“Record”: “6”}
]

Expected Output:
[ “response”: [{“Record”: “1”}, {“Record”: “2”}, {“Record”: “5”}, {“Record”: “4”}, {“Record”: “6”}]
]

25 REPLIES 25

bojanvelevski
Valued Contributor

Hello @thilkip,

You can try and use Group by N snap. Set the group size to 0 and this is what you get:

image

Regards,
Bojan

yes, but Group by is not supported in Ultra tasks

Sorry, I missed that information.

We tried with script as below. it works in design mode but not when we call ultra tasks. When we call ultra tasks the script snap doesnt provide any output

execute : function () {
this.log.info(“Executing Transform Script”);
var outDoc = new LinkedHashMap();
var inDoc=null;
var items = new ArrayList();
var i=0;

	while (this.input.hasNext()) 
	{
		try {
			// Read the next input document, store it a new LinkedHashMap, and write this as an output document.
			// We must use a serializable Java type liked LinkedHashMap for each output instead of a native
			// JavaScript object so that downstream Snaps like Copy can process it correctly.
			inDoc = this.input.next();
			i=i+1;
			items.add(inDoc);

			outDoc.put("index", i);         
			
			if ((!this.input.hasNext()) || (this.input.hasNext()==false))
			{   
				outDoc.put("data", items);
				this.output.write(inDoc, outDoc);
			}
			
		}
		catch (err) {
			var errDoc = new LinkedHashMap();
			errDoc.put("error", err);
			this.log.error(err);
			this.error.write(errDoc);
		}
	}
     
    this.log.info("Script executed");