I am trying to pull data from SFDC & my query looks something like this:
Select X,Y,(Select Z from OpportunityLine) from Opportunity;
Post this I am trying to use dataweave to map it to values.
<dw:transform-message doc:name="Salesforce Opportunity Selection">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload map {
id : $.X,
name : $.Y,
}]]></dw:set-payload>
</dw:transform-message>
How can I grab values from my subquery into the map?
specify a alias in your sql like this:
Select X,Y,(Select Z from OpportunityLine) FOO from Opportunity;
then you can access FOO in your dataweave:
<dw:transform-message doc:name="Salesforce Opportunity Selection">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload map {
id: $.X,
name: $.Y,
foo: $.FOO
}]]></dw:set-payload>
</dw:transform-message>
you can read more about aliases in sql here.
Related
Is it possible to access data with wildcard in dataweave?
I have my payload as Entry_1_m1,Entry_2_m1,Entry_3_m1
Entries will be dynamic there might be other entry as Entry_1_m2,Entry_2_m2,Entry_3_m2.
So I should be able to get the values irrespective of last number that is I should get the value of (Entry_1_m*). Is it possible in dataweave?
%dw 1.0
%input payload application/java
%output application/xml
---
{
Employee:{
empRequest:{
ReqNumber:payload.RequestNumber,
Reasons:{
Entry:payload.Entry_1_m1,
Entry:payload.Entry_2_m1,
Entry:payload.Entry_3_m1
}
}
}
}
Use pluck operator to get list of keys which will help you to map all fields starting with Entry_1_m. Following code worked for me
%dw 1.0
%input payload application/java
%output application/xml
%var keys = (payload pluck $$) filter ($ startsWith 'Entry_1_m')
---
{
Employee:{
empRequest:{
ReqNumber:payload.RequestNumber,
Reasons: {(keys map {
Entry:payload[$]
})}
}
}
}
Hope this helps.
If I have xml like so....
<Root>
<Authority>Water</Authority>
<Sanctions>
<Sanction>
<SanctionCode>11</SanctionCode>
<SanctionDesc>First Sanction</SanctionDesc>
</Sanction>
<Sanction>
<SanctionCode>11</SanctionCode>
<SanctionDesc>Second Sanction</SanctionDesc>
</Sanction>
</Sanctions>
</Root>
Using DataWeave how can I create a json array of Santions using only the SanctionDesc?
I've tried this but it's not right...
%dw 1.0
%output application/json
---
records: payload.Root map {
Authority: $.Authority,
sanctions: $.Sanctions.Sanction map [$.SanctionDesc]
}
I want my output to look like this...
{
"records": [{
"Authority": "Water",
"sanctions": ["First Sanction", "Second Sanction"]
}]
}
Try this
%dw 1.0
%output application/json
---
records: {
Authority: payload.Root.Authority,
sanctions: payload.Root.Sanctions..SanctionDesc
}
Or
%dw 1.0
%output application/json
---
records: {
Authority: payload.Root.Authority,
sanctions: payload.Root.Sanctions.*Sanction map $.SanctionDesc
}
Hope this helps.
Understading the map operator is the key when picking and choosing elements from input payload. Map operator goes through all the array elements on the left hand side and we can pick the values from on right hand side, Giving example from Mulesoft portal
%dw 1.0
%output application/json
users: ["john", "peter", "matt"] map ((firstName, position) -> position ++ ":" ++ upper firstName)
Output:
{
"users": [
"0:JOHN",
"1:PETER",
"2:MATT"
]
}
See link below:
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#map
I am using dataweave, due to sales taxes in each state, I need to assign a each customer from a specific state to a pre-defined user.
However when I create this in dataweave I end up with this as my XML:
<Customer>
<Number>
<Number>
<Number>
<Number>WEB002</Number>
</Number>
</Number>
</Number>
I just need one 'number' node underneath the 'customer' node.
Here is my dataweave:
Customer:{
Number: 'WEB001' when payload.order.billing_address.state == 'Indiana' otherwise
Number: 'WEB002' when payload.order.billing_address.state == 'Kentucky' otherwise
Number: 'WEB003' when payload.order.billing_address.state == 'Illinois' otherwise
Number: 'WEB004'
},
Have tried placing my conditions inside of brackets, no joy, have tried to wrap each condition in brackets, no joy.
Is there a better way to do this?
A very easy and simple way to do that as follows:-
<dw:transform-message doc:name="Transform Message">
<dw:input-payload doc:sample="empty.xml" />
<dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
Customer:
Number: "WEB001" when payload.order.billing_address.state == "Indiana"
otherwise (
"WEB002" when payload.order.billing_address.state == "Kentucky"
otherwise (
"WEB003" when payload.order.billing_address.state == "Illinois"
otherwise "WEB004"))
}
]]></dw:set-payload>
</dw:transform-message>
you can put the mapping between state and number in a map/dictionary which would improve the readability like this:
%var numbers = {
Indiana: 'WEB001',
Kentucky: 'WEB002',
Illinois: 'WEB003'
}
this is what your dataweave would look like:
%dw 1.0
%output application/xml
%var defaultNumber = 'WEB004'
%var numbers = {
Indiana: 'WEB001',
Kentucky: 'WEB002',
Illinois: 'WEB003'
}
---
Customer: {
Number: numbers[payload.order.billing_address.state]
default defaultNumber
}
and here is a screenshot containing sample data and the resulting preview:
I want to set up a key value dictionary from 2 different fields in my payload. My first thought is to use a transform component and dynamically set the key, but this code does not work:
%dw 1.0
%output application/java
---
{
payload.objectnumber: payload.objectid
}
I get this error:
Multiple markers at this line
- Invalid input "payload.", expected conditionalKeyValuePair,
keyValuePair, enclosedExpr or objectEnd
- no viable alternative at input 'payload'
- no viable alternative at input '.'
Any ideas on how to get it working?
Input:
[{
objectnumber: 75,
objectid: "abcdefgh"
}]
Expected Output:
[{
"abcdefgh": 75
}]
Put a variable inside bracket (value) to define a dynamic key. Therefore, you can try this code:
%dw 1.0
%output application/json
---
payload map {
($.objectid): $.objectnumber
}
Try the below code
<dw:transform-message metadata:id="b968b3ec-ed0a-492d-bf32-bcfe0d20e9d0" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
%var objectId = payload.objectid
---
objectId :payload.objectnumber
]]></dw:set-payload>
</dw:transform-message>
JSON Input in Postman:
[
{
"objectnumber":75,
"objectid":"abcdefgh"
}
]
I am getting a field AnnualRevenue in the input, I have to look for the condition that if there is a decimal in that field I want to substing it to a whole number, but if it does not a decimal, it is passed without any transformation. Can one tell me how this could be achived in dataweave in mulesoft.
For another alternative, You can utilize regex:
%dw 1.0
%output application/java
---
{
result: "123.45" replace /(\.[0-9]*)/ with ""
}
Aside from subtring you can use the integer conversion of MEL/java by creating a global MEL function then use it in your dataweave expression.
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def removeDecimal(value) {
return (long) value
}
</global-functions>
</expression-language>
</configuration>
then in your dataweave expression:
%dw 1.0
%output application/json
---
{
"result": removeDecimal(-21.83)
}
Hope it helps.
Cheers
input:
<employees>
<employee>
<name>salumiah-syed</name>
</employee>
</employees>
%dw 1.0
%output application/xml
---
result: (payload.employees.employee.name splitBy "-") [0]
Or
%dw 1.0
%output application/java
---
{
value: "123.45",
result: ("123.45" as :number) as :string {format: "#"}
}
Reference: https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#coerce-to-string