Search code examples
muleanypoint-studiodataweave

Concatenating two arrays in mule


I am trying to concatenate two arrays:

arr1: [{"a":"value"}] 
arr2: [{"b":"value"}]
expected result:[{{"a":"value"},{"b":"value"}}]
vars.arr1 ++ vars.arr2  gives an error when arr1 is null. 

    org.mule.runtime.core.api.expression.ExpressionRuntimeException: "You called the function '+' with these arguments: 
      1: Null (null)
      2: Object 

How should I handle the following scenarios:

  1. when both the arrays are null
  2. when one of the array is null.

Solution

  • Try this if you don't mind getting the empty array when both input arrays are null:

    %dw 2.0
    output application/dw
    
    var arr1 = null
    var arr2 = null
    
    ---
    (arr1 default []) ++ (arr2 default [])