0

incoming payload:

{
    "Categories": [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        }, {
            "ID": "89k873c8",
            "Code": "CTY"
        }
    ]
}

flowVar value is an ArrayList: ([84hkj569],[6j93hl9])

desired output payload:

  {
        "Categories": [
            {
                "ID": "5a873ca3",
                "Code": "CTY"
            }, {
                "ID": "89k873c8",
                "Code": "CTY"
            }, {
                "ID": "84hkj569",
                "Code": "CTY"
            }, {
                "ID": "6j93hl9",
                "Code": "CTY"
            }
        ]
    }

I couldn't find a way to do in dataweave, Would you please help

MoeMan
  • 31
  • 2
  • 9

2 Answers2

0

The following dataweave code should give you what you want:

%dw 1.0
%output application/json
---
{
    Categories: payload.Categories ++ (flowVars.value map {
        "ID": $,
        "Code": "CTY"
    })
}

Here's the configuration from a sample flow which I have used, and the output:

    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    "Categories": [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        }, {
            "ID": "89k873c8",
            "Code": "CTY"
        }
    ]
}]]></dw:set-payload>
        </dw:transform-message>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"><![CDATA[
flowVars.value = new java.util.ArrayList();
flowVars.value.add("84hkj569");
flowVars.value.add("6j93hl9");
return payload;
]]></scripting:script>
        </scripting:component>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    Categories: payload.Categories ++ (flowVars.value map {
        "ID": $,
        "Code": "CTY"
    })
}]]></dw:set-payload>
        </dw:transform-message>

Output:

{
    "Categories": [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        },
        {
            "ID": "89k873c8",
            "Code": "CTY"
        },
        {
            "ID": "84hkj569",
            "Code": "CTY"
        },
        {
            "ID": "6j93hl9",
            "Code": "CTY"
        }
    ]
}
Brad Cooper
  • 379
  • 1
  • 4
  • For some reason I couldn't get the value from the flowVars, it's been defined as java.util.ArrayList(), I'm getting the below error (Type mismatch for 'map' operator found :string, :function required :array, :function) – MoeMan May 03 '18 at 00:58
  • The error seems to suggest to me that `flowVars.value` is a string and not actually an ArrayList as you said. It might sound like a silly question, but how have you initialised this variable? I created a sample flow to test it and created the flowVar with the expression `#[["84hkj569","6j93hl9"]]` which seemed to work for me... – Brad Cooper May 03 '18 at 01:40
  • I'm trying to hook it in the transformer itself as a string, I also tried to have it as a map, I defined it as #[new java.util.ArrayList()], and I add some strings to it, but it doesn't recognize it in dataweave – MoeMan May 03 '18 at 01:55
  • Strange. I've edited my original answer to include an entire flow showing how I'm initialising the flowVar (now using new ArrayList() etc). What version of Mule/Studio are you using? This is Studio 6.4.4 with Mule 3.9.0 – Brad Cooper May 03 '18 at 03:04
  • Would probably need to see your entire flow xml to help any further – Brad Cooper May 03 '18 at 21:34
  • I think the problem is with 3.8.1, if you're inside a foreach, for some reason you won't be able to concatenate Json payload to flowVars. See here: https://stackoverflow.com/questions/45689371/mule-if-flowvar-used-in-dataweave-transformer-payload-converted-to-map – MoeMan May 05 '18 at 03:08
0

Brad Cooper's dataweave code to concatenate JSON payload and flowVars.value works without any issues. Here is the complete example that works with 3.8.1.

Complete Code:

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

    <flow name="sample-dataweaveFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/sample" doc:name="HTTP"/>
        <logger message="Request Received" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-variable variableName="value"><![CDATA[%dw 1.0
%output application/java
---
['84hkj569', '6j93hl9']]]></dw:set-variable>
        </dw:transform-message>
        <logger message="Flow Variable: #[flowVars.value]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    Categories: [
        {
            "ID": "5a873ca3",
            "Code": "CTY"
        }, {
            "ID": "89k873c8",
            "Code": "CTY"
        }
    ]
}]]></dw:set-payload>
        </dw:transform-message>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="Payload: #[payload]" level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    Categories: payload.Categories ++ (flowVars.value map {
        ID: $,
        Code: 'CTY' 
    })
}]]></dw:set-payload>
        </dw:transform-message>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="Final Response: #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

TEST URL:

http://localhost:8081/sample

Console Output:

INFO  2018-05-09 01:08:51,575 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Request Received
INFO  2018-05-09 01:08:51,576 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
INFO  2018-05-09 01:08:51,595 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Flow Variable: [84hkj569, 6j93hl9]
INFO  2018-05-09 01:08:51,596 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
INFO  2018-05-09 01:08:51,620 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Payload: {
  "Categories": [
    {
      "ID": "5a873ca3",
      "Code": "CTY"
    },
    {
      "ID": "89k873c8",
      "Code": "CTY"
    }
  ]
}
INFO  2018-05-09 01:08:51,635 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Final Response: {
  "Categories": [
    {
      "ID": "5a873ca3",
      "Code": "CTY"
    },
    {
      "ID": "89k873c8",
      "Code": "CTY"
    },
    {
      "ID": "84hkj569",
      "Code": "CTY"
    },
    {
      "ID": "6j93hl9",
      "Code": "CTY"
    }
  ]
}