Checking for existence of an array in a mapper

Hello. Im looking for a way of dealing with JSON documents that are sometimes recognised as an array, and sometime not. In the below example, i need to manipulate the “message” element to remove certain messages before passing these on to another web hook. We have achieved this using routers to check for the existence of an array, and they join back. However we need this as an ultra pipeline, so cannot use a join.

the current solution looks like this…

essentially what i am going here is copying the document, routing, array or non array, manipulating and joining back. I need to be able to do all this in a single path in a series of mappers, without using a join. Any help would be greatly appreciated.

You might be able to get around it by using the following expression in a mapper before trying to process the $messages value:

sl.ensureArray($messages)

This way, if it is a single object, it will be a single-entry array. If it is already an arrary, it simply passes through.

5 Likes

Yes, Kory is correct: it’s standard practice to use sl.ensureArray() on such elements. This is particularly the case when dealing with documents that were converted from XML. Unlike JSON, XML doesn’t have an explicit syntax for arrays, so there’s ambiguity when converting an element with one child – it may or may not represent an array with one element.