cancel
Showing results for 
Search instead for 
Did you mean: 

Checking for existence of an array in a mapper

Tommy
New Contributor III

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.
image

the current solution looks like this…

image

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.

1 ACCEPTED SOLUTION

koryknick
Employee
Employee

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.

View solution in original post

2 REPLIES 2

koryknick
Employee
Employee

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.

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.