1
votes

I need to format an incoming payload JSON to a certain manner. The sample payload JSON is given below.

[
  {
    "value": {
      "States": [
        {
          "Name": "New South Wales",
          "Code": "NSW"
        }
      ]
    }
  },
  {
    "value": {
      "States": [
        {
          "Name": "Western Australia",
          "Code": "WA"
        }
      ]
    }
  }
]

The output that I'm trying to get to is given below:

[
  {
    "SystemCode": "STATE",
    "Name": "StateName",
    "Code": "NSW"
  },
  {
    "SystemCode": "STATE",
    "Name": "StateName",
    "Code": "WA"
  }
]

As you can see above, in the output, SystemCode and Name will remain constant, whereas the Code value will change as per the incoming payload. Once the required output is generated, I need to store the same as a JSON in a session variable. How can I achieve this in Mule dataweave 1.0 Please note that I can receive multiple arrays as payload with different codes. Thanks in advance.

2

2 Answers

6
votes

Try this:

%dw 1.0
%output application/json

%var codes = payload..Code

%var baseObj = {
    "SystemCode" : "STATE",
    "Name"       : "StateName"
}
---
codes map (baseObj ++ {"Code": $})

To set it to a session var:

<dw:transform-message>
  <dw:set-session-variable variableName="sessionVarName">
    <![CDATA[
      <YOUR CODE HERE>
    ]]>
  </dw:set-session-variable>
</dw:transform-message>
0
votes

Try this :

%dw 1.0
%output application/json
---
payload.value.States map 
{
    "SystemCode": "STATE",
    "Name": "StateName",
    "Code": $.Code[0]
}